Design Patterns Bloch
Design Patterns Bloch
17-214 1
Administrivia
17-214 2
Outline
I. Creational Patterns
II. Structural Patterns
III. Behavioral Patterns
17-214 3
Pattern Name
17-214 4
Illustration
17-214 5
I. Creational Patterns
1. Abstract factory
2. Builder
3. Factory method
4. Prototype
5. Singleton
17-214 6
1. Abstract Factory
17-214 7
GoF Abstract Factory Illustration
Products
WidgetFactory Client
CreateWindow() Window
CreateScrollBar()
PMWindow MotifWindow
MotifWidgetFactory PMWidgetFactory
CreateWindow() CreateWindow()
CreateScrollBar() CreateScrollBar()
ScrollBar
PMScrollBar MotifScrollBar
17-214 8
2. Builder
17-214 9
GoF Builder Illustration
Director
Builders
RTFReader TextConverter
ParseRTF() AddChar(char)
SetFont(font)
AddParagraph()
while(t = nextToken) {
switch t Type {
CHAR: ASCIIConverter TeXConverter GUITextConverter
builder->AddChar(t.Char) AddChar(char) AddChar(char) AddChar(char)
FONT: GetASCIIText() SetFont(font) SetFont(font)
builder->SetFont(t.Font) AddParagraph() AddParagraph()
PARA: GetTeXText() GetGUIText()
builder->AddParagraph()
}
}
ASCIIText TeXText GUIText
Products
17-214 10
My take on Builder [Effective Java Item 2]
Pizza largeHawaiian =
new Pizza.Builder(LARGE).add(HAM).add(PINEAPPLE).build();
17-214 11
3. Factory Method
17-214 12
Factory Method Illustration
17-214 13
4. Prototype
17-214 14
5. Singleton
17-214 15
Singleton Illustration
// Alternative implementation
public class Elvis {
public static final Elvis ELVIS = new Elvis();
private Elvis() { }
...
}
17-214 16
My take on Singleton
17-214 17
II. Structural Patterns
1. Adapter
2. Bridge
3. Composite
4. Decorator
5. Façade
6. Flyweight
7. Proxy
17-214 18
1. Adapter
17-214 19
Adapter Illustration
17-214 20
2. Bridge
17-214 21
Bridge Illustration
17-214 22
3. Composite
17-214 23
Composite Illustration
17-214 24
4. Decorator
17-214 25
GoF Decorator Illustration
17-214 26
5. Façade
17-214 27
Façade Illustration
√ √ √
√ √ √
17-214 28
6. Flyweight
17-214 29
GoF Flyweight Illustration
17-214 30
7. Proxy
17-214 31
Proxy Illustrations
Virtual Proxy
aTextDocument
anImageProxy
image anImage
fileName
data
in memory
on disk
Client
Server
Proxy
SynchronizedList ArrayList
17-214 32
III. Behavioral Patterns
1. Chain of Responsibility
2. Command
3. Interpreter
4. Iterator
5. Mediator
6. Memento
7. Observer
8. State
9. Strategy
10. Template method
11. Visitor
17-214 33
1. Chain of Responsibility
17-214 34
2. Command
17-214 35
Command Illustration
17-214 36
3. Interpreter
17-214 37
Interpreter Illustration
17-214 38
4. Iterator
17-214 39
Iterator Illustration
Collection<String> c = ...;
17-214 40
5. Mediator
17-214 41
Mediator Illustration
17-214 42
6. Memento
17-214 43
7. Observer
17-214 44
Observer Illustration
17-214 45
8. State
17-214 46
9. Strategy
17-214 47
Strategy Illustration
Arrays.sort(args, comparingInt(String::length));
System.out.println(Arrays.toString(args));
}
17-214 48
10. Template Method
17-214 49
Template Method Illustration
17-214 50
11. Visitor
Probably the trickiest GoF pattern
17-214 51
Visitor Illustration (1/3: machinery)
17-214 52
Visitor Illustration (2/3, eval visitor)
17-214 53
Visitor Illustration (3/3, toString visitor)
17-214 54
More on Visitor
17-214 55
Summary
17-214 56