Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction to
Design Patterns
Design Patterns
Elements of Reusable
Object-Oriented Software
by
Erich Gamma, Richard Helm,
Ralph Johnson, John Vlissides
Addison-Wesley, 1995.
Design Patterns
Elements of Reusable
Object-Oriented Software
by
The Gang
of Four
often called
the Gang of Four
or GoF book
Design Patterns
Elements of Reusable
Object-Oriented Software
by
The Gang
of Four
original,
well-known
book introducing
design patterns
often called
the Gang of Four
or GoF book
Design Patterns
Elements of Reusable
Object-Oriented Software
by
The Gang
of Four
original,
well-known
book introducing
design patterns
often called
the Gang of Four
or GoF book
Design Patterns
s
Example
ed in
present
+
C+ (and
k)
Smalltal
The Gang
of Four
Elements of Reusable
Object-Oriented Software
by
Erich Gamma, Richard Helm,
Ralph Johnson, John Vlissides
Addison-Wesley, 1995.
same erns
tt
a
p
n
desig e GoF
as th a little
th
i
g
w
n
i
t
r
o
bu
ct
a
f
e
r
f
o
t
i
b
Patterns
in Java
Volume 1
A Catalog of Reusable
Design Patterns Illustrated with UML
by
Mark Grand
Wiley, 1998.
ly
h
g
i
h
t
o
N mended
m
o
c
re
3
e
c
r
u
o
s
e
r
F
r
e
o
h
G
t
ano ollows mat
f
r
o
f
k
o
o
b
Addison-Wesley, 1998.
e
c
r
u
o
s
e
r
F
r
e
o
h
G
t
ano ollows mat
f
r
o
f
k
o
o
b
Addison-Wesley, 1998.
e
r
g
A
!
k
o
at bo
height
Person
openDoor
height
Intruder
openDoor
Resident
openDoor
Copyright 2009 Andrew Black
CourseOffering new
studentaddressline2zipcode
11
Misuse of Inheritance
Rectangle
area
length
length:
width
width:
Window
Close
Display
12
Window
area
^ bounds area
Rectangle
bounds
area
width
width:
length:
length
^ width * height
Example of delegation
Now we have two objects:
a Window object and a Rectangle object
13
Window
area
^ bounds area
Rectangle
bounds
area
width
width:
length:
length
^ width * height
13
Window
area
^ bounds area
Rectangle
bounds
area
width
width:
length:
length
^ width * height
13
14
Scope
class
Creational
Structural
Behavioral
factory method
adapter
interpreter
template method
abstract factory
builder
prototype
singleton
object
adapter
bridge
composite
decorator
faade
flyweight
proxy
chain of responsibility
command
iterator
mediator
memento
observer
state
strategy
visitor
16
17
Motivation:
Applicability:
18
19
Singleton
initialize
singletonMethod:
singletonData
singletonData
19
Singleton
initialize
singletonMethod:
singletonData
singletonData
Singleton class
default
new
uniqueInstance
19
Singleton
initialize
singletonMethod:
singletonData
singletonData
Singleton class
default
new
uniqueInstance
19
Singleton
initialize
singletonMethod:
singletonData
singletonData
Singleton class
default
new
uniqueInstance
uniqueinstance ifNil:
[uniqueInstance := super new].
^ uniqueInstance
19
Singleton
initialize
singletonMethod:
singletonData
singletonData
Singleton class
default
new
uniqueInstance
uniqueinstance ifNil:
[uniqueInstance := super new].
^ uniqueInstance
19
Singleton class
defines a default method
is responsible for creating its own
unique instance and maintaining
a reference to it
overrides new
Singleton
the unique instance
overrides initialize
defines application-specific behavior
Collaborations:
Clients access singleton sole through Singleton
classs default method
may also be called current, instance,
uniqueInstance
20
21
Smalltalk Implementation
In Smalltalk, the method that returns the unique instance is
implemented as a class method on the Singleton class. The new
method is overridden.
uniqueInstance is a class instance variable, so that if this class is ever
subclassed, each subclass will have its own uniqueInstance.
22
Singleton class>>default
"Return the unique instance of this class; if it hasnt
!
Proxy
24
25
26
Proxy Structure
27
Proxy Structure
Subject is
a protocol, aka interface
(note: italics)
27
Implementation
Two approaches
1. Implement all of Subjects methods in Proxy. A
few of them will actually have code in Proxy, but
most will just forward the message to RealSubject
aMessage: parameter
! ^ realSubject aMessage: parameter
ProtoObject
In Pharo, ProtoObject is the superclass of
Object. It implements the methods that all
objects really, really, really need to
support.
Object methodDict size ==> 340.
ProtoObject methodDict size ==> 38.
If you use approach 2 to implement a Proxy,
then it should subclass ProtoObject
29
Avoiding Forwarding
!
!
!
!
doesNotUnderstand: aMessage
! realSubjectProtocol includes: aMessage ifFalse: [].
! self become: realSubject.
! ^aMessage sendTo: self
30
Iterator
32
External Iterators
33
Java test
34
Internal Iterators
35
do:!
do:separatedBy: ! do:without:
for keyedCollections
for SequenceableCollections
36
! collect:! collect:thenDo:collect:thenSelect:
for SequenceableCollections
collect:from:to: ! withIndexcollect: !
37
38
partial do
39
max:!min:
or programmable:
detect:! detect:ifNone:
40
Summarizing:
answering a single value
allSatisfy:! anySatisfy:
detectMin:! detectMax:! detectSum:
41
Context
Problem
42
Solution
changes state, all its dependents are notified and updated automatically.
Structure
Subject
Observer
addDependent: anobserver
removeDependent: anobse~er
changed: aSymbol
Concretesubject
subjectstate
0-
- -. - ,
I
I
observerstate
changes state, all its dependents are notified and updated automatically.
protocols
Structure
Subject
Observer
addDependent: anobserver
removeDependent: anobse~er
changed: aSymbol
Concretesubject
subjectstate
0-
- -. - ,
I
I
observerstate
changes state, all its dependents are notified and updated automatically.
protocols
Structure
Subject
Observer
addDependent: anobserver
removeDependent: anobse~er
changed: aSymbol
implements
Concretesubject
subjectstate
0-
- -. - ,
I
I
observerstate
changes state, all its dependents are notified and updated automatically.
protocols
Structure
Subject
Observer
addDependent: anobserver
removeDependent: anobse~er
changed: aSymbol
implements
implements
Concretesubject
subjectstate
0-
- -. - ,
I
I
observerstate
changes state, all its dependents are notified and updated automatically.
protocols
Structure
Subject
Observer
addDependent: anobserver
removeDependent: anobse~er
changed: aSymbol
implements
Concretesubject
subjectstate
0-
- -. - ,
I
I
observerstate
changes state, all its dependents are notified and updated automatically.
protocols
Structure
Subject
Observer
addDependent: anobserver
removeDependent: anobse~er
changed: aSymbol
Concretesubject
subjectstate
0-
- -. - ,
I
I
observerstate
Two Protocols
Pharo Implementation
self changed
Subject
messages
Dependent
messages
Managing dependencies
Subject
messages
aSubject
!
addDependent: aDependent
aSubject
!
removeDependent: aDependent
48
Context:
Problem:
50
Solution
51
Architecture
52
Further Reading
53
54
Intent:
Context:
Problem:
55
myState
TCPEstablished
open
close
acknowledge
TCPState
open
close
acknowledge
TCPListen
open
close
acknowledge
TCPClosed
open
close
acknowledge
56
myState
TCPEstablished
open
close
acknowledge
TCPState
open
close
acknowledge
TCPListen
open
close
acknowledge
Notice that we
have an abstract
class here!
TCPClosed
open
close
acknowledge
56
myState
TCPEstablished
open
close
acknowledge
TCPState
open
close
acknowledge
TCPListen
open
close
acknowledge
TCPClosed
open
close
acknowledge
56
myState
TCPEstablished
open
close
acknowledge
TCPState
open
close
acknowledge
TCPListen
open
close
acknowledge
TCPClosed
open
close
acknowledge
57
myState
TCPEstablished
open
close
acknowledge
TCPState
open
close
acknowledge
TCPListen
open
close
acknowledge
Methods could
be abstract, or
could provide
default behavior
TCPClosed
open
close
acknowledge
57
myState
TCPEstablished
open
close
acknowledge
TCPState
open
close
acknowledge
TCPListen
open
close
acknowledge
TCPClosed
open
close
acknowledge
57
objects in the
rest of the
program
create and interact with
a TCPConnection object
TCPConnection
myState
TCPState
open
close
acknowledge
object
TCPClosed
TCPListen
TCPEstablished
...
object
object
object
58
Sequence Diagram
client object in
the program
TCP
Established
State
TCP
Connection
open
TCP
Listen
State
...
TCP
Closed
State
open
send
send
send
send
59
my_state
State
handle
myState handle
ConcreteState1
handle
ConcreteState2
handle
...
60
61
62
TCP Connection
(cont.)
TCPConnection>>activeOpen
! delegate the open message to the current state.
! self state activeOpen: self
63
TCP Connection
(cont.)
TCPState>>activeOpen: aTCPConnection
!Dont implement an open method.expect the concrete subclasses to
! self subclassResponsibility
and do the same thing for all other messages for TCPState
(that is, TCPState is an abstract class)
!
TCPState subclass: #TCPEstablished
! instanceVariableNames:
! classVariableNames:
! poolDictionaries:
and do the same thing for all other concrete states that you need
(TCPListen state, TCPClosed state, etc.)
64
TCP Connection
(cont.)
TCPEstablishedState>>activeOpen: aTCPConnection
!Do nothing.the connection is already open
! ^self
TCPClosedState >>activeOpen: aTCPConnection
! do the open.invoke the establishConnection method of TCPConnection
! ^aTCPConnection establishConnection
TCPConnection>>establishConnection
! Do the work to establish a connection. Then change state.
! self state: TCPEstablishedState new
65
TCP Connection
(cont.)
TCPEstablishedState>>activeOpen: aTCPConnection
!Do nothing.the connection is already open
! ^self
TCPClosedState >>activeOpen: aTCPConnection
! do the open.invoke the establishConnection method of TCPConnection
! ^aTCPConnection establishConnection
TCPConnection>>establishConnection
! Do the work to establish a connection. Then change state.
! self state: TCPEstablishedState new
TCP Connection
(cont.)
TCPEstablishedState>>activeOpen: aTCPConnection
!Do nothing.the connection is already open
! ^self
TCPClosedState >>activeOpen: aTCPConnection
! do the open.invoke the establishConnection method of TCPConnection
! ^aTCPConnection establishConnection
TCPConnection>>establishConnection
! Do the work to establish a connection. Then change state.
! self state: TCPEstablishedState new
Where are the instance variables? In the context object, or in the state
objects?
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects?
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Where are the instance variables? In the context object, or in the state
objects? Lets leave them in the context object.
Summary
70
71