What Is A Pattern?
What Is A Pattern?
1
You’ve already seen this in Java Observer pattern
• Iterators for Collections
– Also, Enumerators for Hashtables and Vectors
2
Proxy pattern Example of Proxy Pattern
3
Behavioral patterns Singleton pattern
• State • It's important for some classes to have
– Allow an object to alter its behavior when its exactly one instance
internal state changes. The object will appear to
– Many printers, but only one print spooler
change its class.
– One file system
• Visitor
– Represent an operation to be performed on the – One window manager
elements of an object structure. Visitor lets you • Such designs can be limiting
define a new operation without changing the
classes of the elements on which it operates.
Implementing the
The Singleton solution
Singleton method
• Make the class itself responsible for • In Java, just define a final static field
keeping track of its sole instance. Class Singleton {
private Singleton() {…}
• Make constructor private public final static Singleton instance
• Provide static method/field to allow access = new Singleton();
to the only instance of the class …
}
• Java semantics guarantee object is created
immediately before first use
4
Abstract Factory Using an abstract factory
• Different look-and-feels define different
appearances and behaviors for user interface • Get a reference to of type WidgetFactory
“widgets” like scroll bars, windows, and – To an object of the appropriate subtype of
WidgetFactory
buttons.
– Ask the WidgetFactory for a scroll bar, or for a
window
5
State pattern Structure of State pattern
• Have a reference to a state object
– Normally, state object doesn’t contain any
fields
– Change state: change state object
– Methods delegate to state object
6
Double-dispatch
Visitor pattern
• Accept code is always trivial
– Just dynamic dispatch on argument, with
runtime type of structure node taking into
account in method name
• A way of doing double-dispatch
– Dynamic dispatching on the run-time types of
two arguments
– Visitor code invoked depends on run-time type
of both visitor and node
7
acceptAndTraverse methods Accept and traverse
• accept method could be responsible for • Class BinaryPlusOperatorNode {
traversing children void accept(Visitor v) {
– Assumes all visitors have same traversal pattern v->visit(this);
• E.g., visit all nodes in pre-order traversal
lhs->accept(v);
– Could provide previsit and postvisit methods to
allow for more complicated traversal patterns
rhs->accept(v);
• Still visit every node }
• Can’t do out of order traversal …}
• In-order traversal requires inVisit method
8
PreorderVisitor with payload Why patterns?
• Class PreorderVisitor { • Sometimes, patterns are just a cool idea you might
Visitor payload; not have come up with on your own
void visit(BinaryPlusOperatorNode n) { – Learn from others
Pattern hype
• Patterns get a lot of hype and fanatical believers
– We are going to have a design pattern reading group,
and this week we are going to discuss the Singleton
Pattern!
• Patterns are sometimes wrong (e.g., double-
checked locking) or inappropriate for a particular
language or environment
– Patterns developed for C++ have very different
solutions in Smalltalk or Dylan