Design Patterns
Download
Report
Transcript Design Patterns
Design Patterns
CSIS 3701: Advanced Object Oriented Programming
Design Patterns
• Standard “toolbox” of design ideas
– Usually codify solutions to common problems
within system design
• Usually support the major data classes within a
system by helping them communicate
– Identify major data classes
– Identify the design pattern classes to connect them
– Not at overall architecture level
(but often used to implement common components
within an architecture)
Design Patterns
• Creational Patterns
–
–
–
–
–
Abstract Factory
Builder
Factory Method
Prototype
Singleton
• Structural Patterns
–
–
–
–
–
–
–
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
• Behavioral Patterns
–
–
–
–
–
–
–
–
–
–
–
Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor
Design Patterns
• “Design Patterns: Elements of Reusable
Object-Oriented Style”
(Gamma, Helm, Johnson, Vlissides)
• Many are built directly into Java
– Façade pattern interface
– Template pattern abstract methods
– Observer, Command patterns event handlers
–…
Decorator Pattern
• “Attach additional responsibilities to an object
dynamically. Decorators provide a flexible alternative
to subclassing for extending functionality.”
• Example: The JScrollPane visual component.
– Can be “wrapped” around any other
visual component (JTextArea,
JPanel, etc.)
– Contains the other visual component.
– Much more convenient than trying to
define scrolling in every different visual class.
Decorator Pattern
• JScrollPane example:
JTextArea area = new JTextArea(10, 25);
JScrollPane scroll = new JScrollPane(area);
JPanel p = new JPanel();
getContentPane.add(p);
p.add(scroll);
We then add the
JScrollPane to the
JPanel
The JScrollPane
contains the JTextArea
Façade Pattern
• “Provide a unified interface to a set of interfaces in a
subsystem. Façade defines a higher-level interface
that makes a subsystem easier to use.”
• Example:
Every time a new name is added to a NameList, name
should be used to add a new record to a database.
Instead of the developers of putting the SQL and JDBC code
directly in the methods, a separate NameList class provides
methods to create the SQL and JDBC.
Façade Pattern
Façade Pattern
• Often implemented as an interface
– Interface for façade defined in advance
– Specific classes later implement the façade
Iterator Pattern
• Provides way to access elements of an
aggregate object sequentially without
knowing representation
• Example:
User classes would like to get students in a Roster
without having to deal with array, linked list, or
however Roster stores it.
Iterator Pattern
Memento Pattern
• Without violating encapsulation, capture and
externalize an object’s internal state so than the
object can be restored to this state later.
• Example:
Students may change their mind after adding a
course. You need to implement an “undo” button
that undoes up to the last five changes made.
Memento Pattern
• Serialization in Java
– Convert entire state of object to
single string which can be stored
in either a file or a database.
object
Member variables
read
file
Memento Pattern
Observer Pattern
• Define a one-to-many dependency between objects
so that when one object changes state, all its
dependents are notified and updated automatically.
• Example:
The registrar maintains a list of all open sections.
When a section is opened or closed (due to students
dropping or adding), that list must be updated.
Observer Pattern
Observer Design Pattern
• Implemented by Listeners in Java
– Objects register with components (by calling their
addActionListener methods).
– When events occur on components, those objects notified
(by calling their actionPerformed methods).
Command Design Pattern
• “Encapsulate a request as an object, thereby letting
you parameterize clients with different requests”
• Implemeted by Event objects in Java
public class Main
public void actionPerformed(
ActionEvent e) {
…
}
JButton object
ActionEvent
public Object getSource();
Abstract Factory Pattern
• Provide and Interface for creating families of related
or dependent objects without specifying their
concrete classes.
• Example:
Java’s BorderFactory class contains static
methods that return different types of borders for
use by panels. Adding new types of borders just
requires adding new methods, not new classes.
Abstract Factory Pattern