Yoder Patterns PDF
Yoder Patterns PDF
Java/C# Edition
Joseph W. Yoder
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 1
Refactory Affiliates
Dragos Manolescu
Brian Marick
Bill Opdyke
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 2
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 3
1
Design Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 4
Why Patterns?
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 5
Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 6
2
Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 7
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 8
Object-Oriented
Design Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 9
3
Overall Goals
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 10
Class Overview
"Presentation
"Reading Groups
"Exercises
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 11
Java and C# has features that affect how design patterns are
applied
• interfaces
• serialization
• distribution
• concurrency
• GUI (AWT, Swing)
• inner classes
• protection
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 12
4
Outline of Course
What are patterns? – Composite, Chain of
Responsibility, Template Method
More Patterns – Decorator, Null Object, Strategy
How patterns work together
Abstract Factory, Adapter, Builder, Command,
Factory Method, Memento, Observer, Prototype,
Singleton, State
Documenting system designs with patterns
Centralized vs. distributed - Interpreter, Visitor,
Iterator
Bridge, Facade, Flyweight, Mediator, Proxy
Other Patterns and where to find more information
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 13
Notation
Design Patterns Book uses OMT
We use this to show the correlation
Sometimes we use UML which is similar
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 14
More Notation
Class Diagrams:
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 15
5
Yet More Notation
Object Diagrams:
Interaction Diagrams:
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 16
Template Method
Problem: Some classes have a similar algorithm, but it is a
little different for each class.
Solution: Define the skeleton of the algorithm as a method
in a superclass, deferring some steps to subclasses.
AbstractClass
…
templateMethod()
primOperation1();
primOperation1()
primOperation2() …
primOperation2();
ConcreteClass
primOperation1()
primOperation2()
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 17
Template Method
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 18
6
Template Method
(example)
public abstract class View {
public abstract doDisplay();
public void display() { public class ListView extends View {
setFocus(); public void doDisplay() {
doDisplay(); setScrollBarWidth();
resetFocos(); …}
} }
}
public class ButtonView extends View {
public void doDisplay() {
setButtonWidth();
…}
}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 19
Composite
Context:
Developing OO software
Problem:
Complex part-whole hierarchy has lots of similar classes.
Example: document, chapter, section, paragraph.
Forces
• simplicity -- treat composition of parts like a part
• power -- create new kind of part by composing existing ones
• safety -- no special cases, treat everything the same
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 20
Document as a Tree
Book
Section Section
Paragraph Paragraph
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 21
7
Composite
Idea: make abstract "component" class.
Alternative 1: every component has a (possibly
empty) set of components.
Component
Children
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 22
Composite Pattern
Component
*
container
children
Leaf Composite 1
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 23
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 24
8
Component Knows its Composite
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 25
Ensuring Consistency
Solution:
Only public operations that change container are
addComponent/removeComponent
These operations update the container of the
component.
There is no other way to change the container.
Composite addComponent(Component c) {
components.add(c);
c.parent = this;
}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 26
Example: Equipment
Equipment
weight
cost
9
Example: Views and Figures
Big window can contain smaller windows.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 28
Component a Window
x, y, width, height
font, ...
an Applet
Adding Container
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 30
10
More of Container add()
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 31
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 32
Painting
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 33
11
Summary of Composite
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 34
Chain of
Responsibility
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 35
Chain of
Responsibility
Handler
Client
handleRequest()
ConcreteHandler1 ConcreteHandler2
handleRequest() handleRequest()
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 36
12
Chain of
Responsibility
What is a Design
Pattern?
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 38
What is a Design
Pattern?
Details of implementing pattern depend on language and
environment.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 39
13
Parts of a Pattern
(Alexander)
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 40
Parts of a Pattern
Examples:
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 41
Parts of a Pattern
(Gamma et. al.)
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 42
14
GoF Design Patterns
Creational patterns
Abstract factory
Builder Behavioral Patterns
Prototype Command
Singleton Interpreter
Iterator
Structural patterns Mediator
Adapter Memento
Bridge Observer
Composite State
Decorator Strategy
Facade Template Method
Flyweight Visitor
Proxy
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 43
Decorators
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 44
Decorator Structure
Element
specialized 1
operations
Primitive Decorator 1
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 45
15
Decorator Example
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 46
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 47
Strategy Pattern
Define a family of algorithms, encapsulate each one, and
make them interchangeable.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 48
16
Strategy Pattern
For procedural languages you would have conditional
code spread throughout your application for dealing
with special cases.
onDisplayButton()
case OS of:
‘NT’ : setButtonWidth: 100;
‘UNIX’: setButtonWidth: 125;
...
onMousePressed()
case OS of:
‘NT’ : setButtonShadowWidth: 10;
‘UNIX’: setButtonShadowWidth: 15;
...
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 49
Strategy
Context AbstractStrategy
doIt doItInContext
ConcreteStrategy
doItInContext
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 50
Strategy
(an example)
Client ButtonDisplay
*
-attr1
-attr2 #setButtonWidth(in width : int)
*
+doSomethingUseful() #setButtonShadowWidth(in width : Integer)
#myPrivateDraw() #buttonWidth() : int
#shadowWidth() : int
NTButtonDisplay UNIXButtonDisplay
#buttonWidth() : int
#shadowWidth() : int
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 51
17
Moving Code
“Refactoring”
To move a function to a different class, add an argument
to refer to the original class of which it was a member
and change all references to member variables to use
the new argument.
If you are moving it to the class of one of the arguments,
you can make the argument be the receiver.
Moving function f from class X to class B
class X {
int f(A anA, B aB){
return (anA.size + size) / aB.size;
} ...
class B {
int f(A anA, X anX){
return (anA.size + anX.size) / size;
} ...
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 52
Moving Code
f(a, b, c)
Car {if this.x > a then
You can also pass in a this.x = a+b
parameter object which else if this.x < a then
this.y = a+b
gives the algorithm all of else
this.x = a+b }
the values that it will need.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 53
NullObject
$XWKRU Bobby Woolf, PLoPD 3
,QWHQW
• provide surrogate for another object that shares same interface
• usually does nothing but can provide default behavior
• encapsulate implementation decisions of how to do nothing
6WUXFWXUH
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 54
18
Design Patterns
Teaching
• help novices learn to act like experts
Design
• vocabulary for design alternatives
• help see and evaluate tradeoffs
Documentation
• vocabulary for describing a design
• describes "why" more than other techniques
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 55
Review
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 56
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 57
19
How Patterns Work Together
Some patterns are commonly used together
Creational patterns:
Some objects have to create other objects.
How can we parameterize them with the kind of
objects that they create?
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 58
Creational Patterns
Factory Method
Factory Object
Abstract Factory
Builder
Prototype
Singleton
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 59
Factory Method
AFooFactory Foo
+Xfactory() xFunction()
+getClass(value):Foo yFunction()
FooA FooB
xTemplate yTemplate
yTemplate
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 60
20
Factory Method
Advantages:
can change class of product in subclass
can produce easier to read functions
Disadvantages:
slower, bulkier
harder to read ALL the code
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 61
Factory Object
Problem with factory method -- have to create subclass
to parameterize.
Example
Figure
FigureFactory
new
LineFigureFactory RectangleFigureFactory
ElipseFigureFactory
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 63
21
Applicability
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 64
Prototype
Making a class hierarchy of factories seems wasteful.
The parameters of an object can be as important as its class.
Solution:
Use any object as a factory by copying it to make a new
instance.
Advantages
Don't need new factory hierarchy.
Can make new "class" by parameterizing an object
Disadvantages
Requires robust copying
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 65
Prototype
Problem: a "chapter" or a "section" is a set of
objects, not a single object. Users want to
"create a new chapter". How should system
create set of objects?
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 66
22
Prototype
DocumentComponent
Clone
Paragraph Composite
title, level DocumentDecorator
TitleDecorator NumberDecorator
title number
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 67
Abstract Factory
Solution:
Make a single object that can make any of the products.
WidgitFactory
CreateScrollBar
ScrollBar CreateWindow
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 68
Builder
Complex objects require a lot of work to make.
Solution:
Factory must keep track of partly built product.
Client specifies product by performing series of
operations on factory.
WindowBuilder
Client AddScrollBar
AddButton
GetWindow
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 69
23
Implementing Builder
Builder can make components using
• Factory Method
• Singleton (to come)
• Abstract Factory, or
• Prototype
WindowBuilder WidgitFactory
AddScrollBar CreateScrollBar
AddButton CreateWindow
GetWindow
MotifWidgetFactory PMWidgetFactory
CreateScrollBar CreateScrollBar
CreateWindow CreateWindow
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 70
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 71
Singleton
What if you want to make sure that a class has only one
instance?
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 72
24
Singleton in Java
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 73
Summary
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 74
Learn Memento
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 75
25
State Pattern
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 76
State Pattern
Context State
+request() xHandle()
yHandle()
ConcreteStateA ConcreteStateB …
xTemplate yTemplate
yTemplate
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 77
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 78
26
State
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 79
State
Figure
Display
Drawing
current tool
DrawingController Tool
SelectionTool CreationTool
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 80
Observer Pattern
Intent: Define a one-to-many dependency between
objects so that when one object changes state, all its
dependents are notified and updated automatically.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 81
27
Observer Pattern
Intent: Define a one-to-many dependency between objects
so that when one object changes state, all its dependents
are notified and updated automatically.
Observable - class Observer - interface
observer/dependent Observer
Subject
addDependent update
removeDependent
notify
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 82
Observer Pattern
Subject observer/dependent Observer
addDependent, update
etc.
Figure
display
update
Drawing
defaultTools
current tool
DrawingController Tool
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 83
Event Handling
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 84
28
Using Observer
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 85
Observer in Java
Observer is an interface.
Observable is a class that implements the ability to keep
track of a set of Observers.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 86
Listening instead of
Observing
EventSource is a subject
EventListener is an observer
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 87
29
Different Kinds of
Listeners
ActionListener
actionPerformed(ActionEvent)
ComponentListener
componentResized(ComponentEvent)
componentMoved(ComponentEvent)
componentShown(ComponentEvent)
componentHidden(ComponentEvent)
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 88
Applet is a “Listener”
Button has methods
addActionListener()
processActionEvent() a Window
Applet registers with Button.
When Button processes action event, an Applet
it calls applet
a Button a TextField
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 89
Memento
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 90
30
Memento
state = m.getState()
return new Memento(state)
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 91
Adapter
Convert the interface of a class into another
interface clients expect. Adapter lets
classes work together that couldn't
otherwise because of incompatible
interfaces.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 92
Command
Intent
Encapsulate a request as an object, thereby
letting you parameterize clients with
different requests, queue or log requests,
and support undoable operations.
Menus often do this.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 93
31
Summary
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 94
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 95
Disadvantages
• program is spread out,
+ harder to understand
+ harder to replace algorithm
• state of object can change, but class can not
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 96
32
The Interpreter Pattern
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 97
Interpreter
Context
Client AbstractExpression *
interpret(Context)
TerminalExpression NonterminalExpression 1
interpret(Context) interpret(Context)
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 98
Spreadsheet Rules
Grammar is
expression ::= expression1 ‘+’ expression |
expression1 ‘-’ expression |
expression1 ::= expression ‘*’ expression |
expression ‘/’ expression |
number | cellID |
‘Subtotal(‘ range ‘)’
range ::= cellID ‘:’ cellID
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 99
33
Spreadsheet Objects
new PlusExpression( new CellExpression(3,2),
new CellExpression(3,3) )
PlusExpression
CellExpression CellExpression
row = 3 row = 3
column = 2 column = 3
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 100
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 101
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 102
34
Applying the Interpreter Pattern
Step 3: Define constructors for making expression tree
Expression(Expression e1, Expression e2) {
operand1 = e1;
operand2 = e2;
}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 103
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 104
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 105
35
Visitor pattern
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 106
Interpreter To Visitor
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 107
value
valueOf
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 108
36
Double Dispatch and Visitor
Double Dispatch – Effectively, the Visitor pattern lets you
add operations to classes without changing them.
Visitor achieves this by using a well known technique
called double-dispatch. Double dispatch operation gets
executed is dependent upon the kind of request and the
type of the receiver. (Search google for Double
Dispatch and Java).
1 + 4.5 = 5.5
• Double dispatch can be used to coerce the right type
Integer
+ (Number aNumber)
return aNumber.addFromInteger(this)
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 109
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 110
Related Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 111
37
Iterator
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 112
Using Enumeration-style
Iterators
public void printEmployees (Employees emp)
{
for (e = emp.employees(); e.hasMoreElements(); )
{
Employee currentEmployee
= (Employee) e.nextElement();
currentEmployee.print();
}
}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 113
Using Iterator-style
Iterators
public void printEmployees (Employees emp)
{
for (i = emp.employees(); i.hasNext(); )
{
Employee currentEmployee
= (Employee) i.next ();
currentEmployee.print();
}
}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 114
38
The Enumeration
Interface
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 115
The Iterator
Interface
public boolean hasNext();
public Object next ();
public void remove();
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 116
Variations on Iterator
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 117
39
An Adapter on an
Enumerator
class EnumerationAdapter implements Iterator
{
private Enumeration e;
public EnumerationAdapter(Enumeration e)
{ this.e = e;}
public boolean hasNext() { return e.hasNextElement(); }
public Object next() { return e.nextElement(); }
public void remove() {}
}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 118
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 119
The Stack
Iterator
Iterator
Iterator
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 120
40
External Tree Iterator
class TreeIterator {
public next() {
if (stack.isEmpty()) return;
stack.push(stack.top().currentItem().children());
while (stack.top().isDone()) {
stack.pop();
stack.top().Next();}}
ElementType currentItem() {
return stack.top().currentItem();}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 121
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 122
1: The Components
If the component handles traversal, it looks like:
public Object accept(Visitor visitor) {
visitor.visitA(this);
for (Enumeration e = children();
e.hasMoreElements) {
item = (Item) e.nextElement();
item.accept(visitor);
Otherwise, it looks like
public Object accept(Visitor visitor) {visitor.visitA(this);}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 123
41
2: The Visitor
If the visitor handles iteration, it looks like:
public Object visitA(ComponentA c) {
// do something with c
for (Iterator i = c.children();
!i.isDone(); I.next())
{
((Component) i.currentItem()).accept(visitor);
}
}
Otherwise Visitor visitA just interacts with componentA.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 124
3: An Iterator
If client calls Iterator, it looks like:
visitor = new ConcreteVisitor.
for (Iterator i = component.iterator();
!i.isDone(); i.Next())
{
((Component) i.currentItem()).accept(visitor);
}
}
Otherwise, the client looks like:
ConcreteVisitor visitor;
treeRoot.accept(visitor);
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 125
Tradeoffs
1: The Components
2: The Visitor
3: An Iterator
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 126
42
Review
Patterns interact:
object can play different roles in different patterns
patterns can be alternatives
one pattern can set up another pattern
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 127
Next Session
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 128
Proxy
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 129
43
Proxy
Client Subject
request
...
realSubject
RealSubject Proxy
request request
realSubject->Request()
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 130
Proxy
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 131
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 132
44
How to Hide Information
Hiding the classes in one module from another makes
changes easier. Some things that help are:
• Abstract classes
• Builder (or Abstract Factory)
Hide classes of products that will be used by other
module in the builder.
Example: window builder, code generator
• Adapter
Hide class being used inside adapter
• Bridge and Facade
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 133
Bridge
What do you do if both an abstraction and its
implementation vary?
Window
XIconWindow WIconWindow
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 134
Bridge
Decouple an abstraction from its implementation so
that the two can vary independently.
Window WindowImp
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 135
45
Bridge and Builder
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 136
Component ComponentPeer
Abstraction Implementation
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 137
Standard Questions
for Bridge
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 138
46
Building the Bridge
a Window
an Applet a WindowPeer
A ContainerPeer
a Button a TextField
a ButtonPeer a TextFieldPeer
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 139
How a Button
Creates a Peer
/**
* Creates the peer of the button. This peer allows us to
* change the look of the button without changing its functionality.
*/
public void addNotify() {
peer = getToolkit().createButton(this);
super.addNotify();
}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 140
How a List
Creates a Peer
/**
* Creates the peer for the list. The peer allows us to modify the
* list's appearance without changing its functionality.
*/
public void addNotify() {
peer = getToolkit().createList(this);
super.addNotify();
synchronized (this) {
visibleIndex = -1;
}
}
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 141
47
Flyweight
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 142
column
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 143
Flyweight
flyweight Flyweight
FlyweightFactory
pool intrinsic
getFlyWeight(key) operation(extrinsic)
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 144
48
Flyweight for CAD
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 145
flyweight Cell
CellFactory
pool
getCell(key) display(loc,connects)
register(key,Cell)
CompositeCell Transistor
ports, locations size
display(loc,connects) display(loc,connects)
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 146
Facade
Provide a unified interface to a set of interfaces in a
subsystem. Facade defines a higher-level interface that
makes the subsystem easier to use.
components
Figure
DrawingView SelectionDrawing
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 147
49
Mediator
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 148
Mediator
InsurancePolicy
Mediator
Colleagues Procedure Worker
CustomerHistory
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 149
Mediator
Business Rule
Mediator
Procedure
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 150
50
Not Mediator
InsurancePolicy
Worker
CustomerHistory
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 151
Mediators
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 152
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 153
51
Summary
You have now seen all of the Design Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 154
Bird on Patterns
Learn the patterns
and then forget
‘em
-- Charlie Parker
http://www.hillside.net
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 155
Silver Buckshot
There are no silver bullets
…...Fred Brooks
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 156
52
UIUC Patterns Group
Software Architecture Group
Ralph Johnson’s Group
• Objects
• Reuse
• Frameworks
• Adaptive Architecture
• Components
• Refactoring
• Evolution
• Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 157
Our Perspective
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 158
Next Session
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 159
53
Other Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 160
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 161
Pattern Language
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 162
54
Mapping Objects To Persistence
Pattern Language
Pattern Name Description
Persistent Layer Provide a layer for mapping your objects to the RDBMS or other data source.
CRUD All persistent object need, at a minimum, create, read, update, and delete
operations.
SQL Code Defines the actual SQL Code that takes the values from the RDBMS or other
data source and retrieves them for the object’s use and vice-versa. It is where
you define the CRUD operations.
Attribute Mapping Maps the values between the database values and attributes. This pattern also
Methods handles complex object mappings. Populates the object(s) with the row values.
Type Conversion Works with Map Attributes to translates values from the database to the
appropriate object types and vice-versa. Insures data integrity.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 163
Change Manager Keeps track of when an object’s values have been changed for maintaining
consistency with the database. It determines the need to write the values to a
database table or not.
OID Manager Generates Unique Keys for the Object Ids during an insert.
Table Manager Manages the mappings from an object to its database table(s).
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 164
Security Patterns
Pattern Name Description
Single Access Providing a common security module and
Point a single way to log into the system.
Check Point Organizing security checks and their repercussions.
Roles Organizing users with similar security privileges.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 165
55
Analysis Patterns
David Hay, Data Model Patterns: Conventions of Thought
Dorset House Publishing, 1996 ISBN 0-932633-29-3
Martin Fowler, Analysis Patterns, Addison-Wesley, 1997
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 166
How Patterns
Fit Together
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 167
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 168
56
Methods and Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 169
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 170
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 171
57
Patterns
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 172
Writing Patterns
You should write patterns because
you will learn a lot about patterns
you probably use some patterns that haven’t been
documented yet
you meet a lot of good people that way
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 173
Finding Patterns
Patterns come to those who wait -- must have time for
reflection.
Patterns come to those who are prepared -- must have
experience in domain of problem.
Patterns are refined in fire -- must have readers who
criticize.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 174
58
How to Find Patterns
Look for a solution and document it.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 175
Writers’ Workshop
Excellent way to get feedback on pattern.
Say what you like before you say what you don’t like.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 176
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 177
59
How to Learn New Patterns
A pattern is usually hard to understand if you don’t need
it and have never used it. Don’t worry, just get the
big picture.
It isn't hard!
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 178
Further information
http://hillside.net
[email protected]
[email protected]
[email protected]
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 179
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 180
60
Design Patterns vs. Frameworks
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 181
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 182
Conclusion
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 183
61
Evolution of Object-Oriented Systems
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 184
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 185
Adaptive Object-Models
Separates what changes from what doesn’t.
Architectures that can dynamically adapt to new user
requirements by storing descriptive (metadata) information
about the business rules that are interpreted at runtime.
Sometimes called a "reflective architecture" or a "meta-
architecture ".
Highly Flexible – Business people
(non-programmers) can change it too.
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 186
62
PLoP Conferences
www.hillside.net
EuroPLoP™ 2003
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 187
Summary
You have been introduced and should recognize many design patterns:
Abstract Factory, Adaptor, Bridge, Builder, Chain of Responsibility,
Command, Composite, Decorator, Façade, Factory Method, Flyweight,
Interpreter, Iterator, Mediator, Memento, Null Object, Observer, Prototype,
Proxy, Singleton, State, Strategy, Template Method, Visitor
You should also be familiar with some other types of patterns and how to find
more information about them.
• Analysis Patterns • Process Patterns
• Architectural Patterns • Persistence Patterns
• Coding Patterns • Security Patterns
• GUI Patterns •…
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 188
That’s All
Copyright 1998-2003 Joseph W. Yoder, Brian Foote & Ralph Johnson. Day 1 -- Slide 189
63