Design Patterns
Design Patterns
1
What are Design Patterns?
2
What are Design Patterns?
• System Requirements:
– Portal between rooms
– Must be able to open and close
• Solution:
– Build a door
4
Real-World Example: the “door” pattern
6
Advantages
• Teamwork
– Members of a team have a way to name and discuss
the elements of their design
7
What design patterns are not
8
References and resources
10
Software Example: A Text Editor
• A WYSIWYG editor
• Text and graphics can be freely mixed
• Graphical user interface
• Toolbars, scrollbars, etc.
10
The Game
11
Problem: Document Structure
character picture
glyph G g glyph
line glyph
(composite)
column glyph
(composite)
14
Logical Object Structure
15
Diagram
Glyph
draw()
n
intersects(int x,int y)
…
children
… … …
16
Composite Pattern
19
Composites
17
Problem: Supporting Look-and-Feel Standards
19
Abstract Object Creation
GuiFactory
CreateCharacter()
CreateRectangle()
MacFactory WindowsFactory …
CreateCharacter() { CreateCharacter() {
CreateRectangle() { CreateRectangle() {
Glyph
Character
MacChar WinChar …
22
Factory Pattern
• A class which
– Abstracts the
creation of a
family of objects
– Different
instances provide
alternative
implementations
of that family
• Note
– The “current” factory is still a global variable
– The factory can be changed even at runtime 23
Problem: Spell Checking
• Considerations
– Spell-checking requires traversing the document
• Need to see every glyph, in order
• Information we need is scattered all over the document
• Iterators
– Hide the structure of a container from clients
– A method for
• pointing to the first element
• advancing to the next element and getting the current
element
• testing for termination
Iterator i = composition.getIterator();
while (i.hasNext()) {
Glyph g = i.next();
25
do something with Glyph g;
Diagram
Iterator
hasNext()
next()
PreorderIterator ListIterator
hasNext() hasNext()
next() next()
26
Notes
– Ugly // do nothing
} else if (…) …
– Change body whenever the
}
class hierarchy of Glyph
27
changes
Visitors
• The idea
– Separate traversal from the action
– Have a “do it” method for each element type
• Can be overridden in a particular traversal 28
Diagram
Glyph Visitor
accept(Visitor) visitChar(Character)
… visitPicture(Picture)
visitLine(Line)
Line
Character Picture …
accept(Visitor v) {
accept(Visitor v) { accept(Visitor v) {
v.visitLine(this);
v.visitChar(this); } v.visitPicture(this); }
for each c in children
c.accept(v) } 29
Visitor Pattern
Visitor Pattern
33
Logical Object Structure
30
Java Code
… // do nothing }
… 31
void accept(Visitor vis) {
} Prof. Majumdar CS 130 Lecture 6
Java Code
• Design issues
– Layout is complicated
– No best algorithm
• Many alternatives, simple to complex
34
The Core Issue
• Formatting is complex
– We don’t want that complexity to pollute Glyph
– We may want to change the formatting method
35
Formatting Examples
Glyph FormatSimple
draw() Compose()
Glyph::insert(g)
intersects(int x,int y) formatter.Compose() …
insert(Glyph)
1 Formatter
Compose()
…
FormatJustified
Composition formatter
draw()
Compose()
intersects(int x, int y)
composition …
insert(Glyph g)
37
Not clear?
42
Strategy Pattern
43
Strategies
Glyph FormatSimple
draw() Compose()
Glyph::insert(g)
intersects(int x,int y) formatter.Compose() …
insert(Glyph)
1 Formatter
Compose()
…
FormatJustified
Composition formatter
draw()
Compose()
intersects(int x, int y)
composition …
insert(Glyph g)
37
Formatter
Formatter-
generated
Glyphs
Formatter
38
Design Patterns Philosophy
• Encapsulate variation
40
Acknowledgements
41