SlideShare ist ein Scribd-Unternehmen logo
1 von 112
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
Selected Design Patterns
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
S.Ducasse 3
Goal
• What are patterns?
• Why?
• Patterns are not god on earth
• Example
S.Ducasse 4
Design Patterns
• Design patterns are recurrent solutions to design
problems
• They are names
• Composite,Visitor, Observer...
• They are pros and cons
S.Ducasse 5
Christoffer Alexander
“The Timeless Way of Building”, Christoffer Alexander,
Oxford University Press, 1979, ISBN 0195024028
More advanced than what is used in computer science
only the simple parts got used.
pattern languages were skipped.
From Architecture
S.Ducasse 6
Why Patterns?
Smart
Elegant solutions that a novice would not think of
Generic
Independent on specific system type, language
Well-proven
Successfully tested in several systems
Simple
Combine them for more complex solutions
There are really stupid patterns (supersuper) in some
books so watch out!!!
S.Ducasse 7
Reusable solutions to common problems
based on experiences from real systems
Names of abstractions above class and object level
a common vocabulary for developers
Handling of functional and non-functional aspects
separating interfaces/implementation, loose coupling
between parts, …
A basis for frameworks and toolkits
basic constructs to improve reuse
Education and training support
Patterns provide...
S.Ducasse 8
Pattern name
Increase of design vocabulary
Problem description
When to apply it, in what context to use it
Solution description (generic !)
The elements that make up the design, their relationships,
responsibilities, and collaborations
Consequences
Results and trade-offs of applying the pattern
Elements in a Pattern
S.Ducasse 9
Composite
• Compose objects into tree structures to represent part-
whole hierarchies.
• Composite lets clients treat individual objects and
compositions of objects uniformly
S.Ducasse 10
Composite Intent
Intent: Compose objects into tree structures to
represent part-whole hierarchies. Composite lets clients
treat individual objects and compositions of objects
uniformly
S.Ducasse 11
Composite Pattern Motivation
S.Ducasse 12
Composite Pattern Applicability
Use the Composite Pattern when :
you want to represent part-whole hierarchies of objects
you want clients to be able to ignore the difference
between compositions of objects and individual objects.
Clients will treat all objects in the composite structure
uniformly
S.Ducasse 13
Composite Pattern Possible Design
S.Ducasse 14
Composite Pattern Participants
Component (Graphic)
declares the interface for objects in the composition
implements default behavior for the interface common to
all classes, as appropriate
declares an interface for accessing and managing its child
components
Leaf (Rectangle, Line,Text, ...)
represents leaf objects in the composition. A leaf has no
children
defines behavior for primitive objects in the composition
S.Ducasse 15
Composite Pattern
Composite (Picture)
defines behaviour for components having children
stores child components
implements child-related operations in the Component
interface
Client
manipulates objects in the composition through the
Component interface
S.Ducasse 16
Composite Pattern Collaborations
Clients use the Component class interface to interact
with objects in the composite structure.
Leaves handle requests directly.
Composites forward requests to its child components
Consequences
defines class hierarchies consisting of primitive and
composite objects.
Makes the client simple. Composite and primitive objects
are treated uniformly. (no cases)
Eases the creation of new kinds of components
Can make your design overly general
S.Ducasse 17
An Alternate Structure
Again structure is not intent!
S.Ducasse 18
Queries...
• To be able to specify different queries over a repository
q1 := PropertyQuery property: #HNL with: #< value: 4.
q2 := PropertyQuery property: #NOM with: #> value: 10.
q3 := MatchName match:‘*figure*’
• Compose these queries and treat composite queries as one
query
• (e1 e2 e3 e4 ... en)((q1 and q2 and q4) or q3) -> (e2 e5)
• composer := AndComposeQuery with: (Array with: q1 with: q2 with:
q3)
S.Ducasse 19
A Possible Solution
S.Ducasse 20
In Smalltalk
• Composite not only groups leaves but can also contain
composites
• In Smalltalk add:, remove: do not need to be declared
into Component but only on Composite.This way we
avoid to have to define dummy behavior for Leaf
S.Ducasse 21
CompositeVariations
• Use a Component superclass to define the interface
and factor code there.
• Consider implementing abstract Composite and Leaf (in
case of complex hierarchy)
• Only Composite delegates to children
• Composites can be nested
• Composite sets the parent back-pointer (add:/remove:)
S.Ducasse 22
CompositeVariations
• Can Composite contain any type of child? (domain issues)
• Is the Composite’s number of children limited?
• Forward
– Simple forward. Send the message to all the children and merge the
results without performing any other behavior
– Selective forward. Conditionally forward to some children
– Extended forward. Extra behavior
– Override. Instead of delegating
S.Ducasse 23
Other Patterns
• Composite andVisitors
• Visitors walks on structured objects
• Composite and Factories
• Factories can create composite elements
S.Ducasse 24
Patterns...
S.Ducasse 25
Creational Patterns
Instantiation and configuration of classes and objects
Structural Patterns
Usage of classes and objects in larger structures, separation
of interfaces and implementation
Behavioral Patterns
Algorithms and division of responsibility
Concurrency
Distribution
Security
Categories of Design Patterns
S.Ducasse 26
Some Creational Patterns
Abstract factory
Builder
Factory Method
Prototype
Singleton
S.Ducasse 27
Some Structural Patterns
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
S.Ducasse 28
Some Behavioral Patterns
Chain of responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor
S.Ducasse 29
Alert!!! Patterns are invading
• Design Patterns may be a real plague!
• Do not apply them when you do not need them
• Design Patterns make the software more complex
– More classes
– More indirections, more messages
• Try to understand when NOT applying them!
S.Ducasse 30
About Pattern Implementation
This is POSSIBLE implementation not a definitive one
Do not confuse structure and intent!!!
Patterns are about INTENT
and TRADEOFFS
S.Ducasse 31
Singleton
Ensure that a class has only
one instance, and provide a
global point of access to it
S.Ducasse 32
The Singleton Pattern
• Intent: Ensure that a class has only one instance, and
provide a global point of access to it
• Problem: We want a class with a unique instance.
• Solution: We specialize the #new class method so that
if one instance already exists this will be the only one.
When the first instance is created, we store and return
it as result of #new.
S.Ducasse 33
Singleton Possible Structure
S.Ducasse 34
The Singleton Pattern
|aLan|
aLan := NetworkManager new
aLan == LAN new -> true
aLan uniqueInstance == NetworkManager new -> true
NetWorkManager class
instanceVariableNames: 'uniqueInstance '
NetworkManager class>>new
self error:‘should use uniqueInstance’
NetworkManager class>>uniqueInstance
uniqueInstance isNil
ifTrue: [ uniqueInstance := self basicNew initialize].
^uniqueInstance
S.Ducasse 35
The Singleton Pattern
• Providing access to the unique instance is not always
necessary.
• It depends on what we want to express.The difference
between #new and #uniqueInstance is that #new
potentially initializes a new instance, while
#uniqueInstance only returns the unique instance (there
is no initialization)
• Do we want to communicate that the class has a
singleton? new? defaultInstance? uniqueInstance?
S.Ducasse 36
Implementation Issues
• Singletons may be accessed via a global variable (ex:
NotificationManager uniqueInstance notifier).
SessionModel>>startupWindowSystem
“Private - Perform OS window system startup”
Notifier initializeWindowHandles.
...
oldWindows := Notifier windows.
Notifier initialize.
...
^oldWindows
• GlobalVariable or Class Method Access
–GlobalVariable Access is dangerous: if we reassign Notifier we lose all
references to the current window.
–Class Method Access is better because it provides a single access point.
This class is responsible for the singleton instance (creation,
initialization,...).
S.Ducasse 37
Implementation Issues
Persistent Singleton: only one instance exists and its
identity does not change (ex: NotifierManager inVisual
Smalltalk)
Transient Singleton: only one instance exists at any
time, but that instance changes (ex: SessionModel in
Visual Smalltalk, SourceFileManager, Screen in
VisualWorks)
Single Active Instance Singleton: a single instance is
active at any point in time, but other dormant instances
may also exist. Project inVisualWorks
S.Ducasse 38
Implementation Issues
classVariable or class instance variable
classVariable
One singleton for a complete hierarchy
Class instance variable
One singleton per class
S.Ducasse 39
Access?
In Smalltalk we cannot prevent a client to send a
message (protected in C++).To prevent additional
creation we can redefine new/new:
Object subclass: #Singleton
instanceVariableNames:‘uniqueInstance’
classVariableNames:‘’
poolDictionaries:‘’
Singleton class>>new
self error:‘Class ‘, self name,‘ cannot create new
instances’
S.Ducasse 40
Access using new: not good idea
Singleton class>>new
^self uniqueInstance
The intent (uniqueness) is not clear anymore! New is
normally used to return newly created instances.The
programmer does not expect this:
|screen1 screen2|
screen1 := Screen new.
screen2 := Screen uniqueInstance
S.Ducasse 41
Favor Instance Behavior
When a class should only have one instance, it could be
tempting to define all its behavior at the class level. But
this is not good:
Class behavior represents behavior of classes:“Ordinary
objects are used to model the real world. MetaObjects
describe these ordinary objects”
Do not mess up this separation and do not mix domain
objects with metaconcerns.
What’s happens if later on an object can have multiple
instances?You have to change a lot of client code!
S.Ducasse 42
Time and not Scope
Singleton is about time not access
time: only one instance is available at the same time
access: can’t you add an instance to refer to the object?
Singleton for access are as bad as global variables
Often we can avoid singleton by passing/referring to the
object instead of favoring a global access point
It is worth to have one extra instance variable that refers
to the right object
S.Ducasse 43
Visitor
Represent an operation to
be performed on the
elements of an object structure in
a class separate from the
elements themselves.Visitor
lets you define a new operation
without changing the classes of the
elements on which it operates.
S.Ducasse 44
Intent: Represent an operation to be performed on the
elements of an object structure in a class separate from
the elements themselves.Visitor lets you define a new
operation without changing the classes of the elements
on which it operates.
Visitor Intent
S.Ducasse 45
Visitor Possible Structure
S.Ducasse 46
Whenever you have a number of items on which you
have to perform a number of actions, and
When you ‘decouple’ the actions from the items.
Examples:
the parse tree (ProgramNode) uses a visitor for the
compilation (emitting code on CodeStream)
GraphicsContext is a visitor forVisualComponents,
Geometrics, and some other ones (CharacterArray, ...)
Rendering documents
When to use aVisitor
S.Ducasse 47
So all our problems are solved, no?
Well...
when to use a visitor
control over item traversal
choosing the granularity of visitor methods
implementation tricks
Applying theVisitor
S.Ducasse 48
Use aVisitor:
when the operations on items change a lot.
Do not use a visitor:
when the items you want to visit change a lot.
Question: But how do we know what to choose up-
front?
When to Use aVisitor
S.Ducasse 49
Language to deal with arithmetic expressions.
It supports one kind of number, and has +, *, (, )
We want to evaluate expressions, and print them.
Example:
1 + 1
result: 1 + 1 = 2
((4 * 2) * (3 + 5)) * 3
result: (4 * 2 * (3 + 5)) * 3 = 192
...
Visitor Toy Example
S.Ducasse 50
Visitor Toy Example: ParseTree
S.Ducasse 51
Two solutions:
add methods for evaluating, printing, ... on Expression and
its subclasses
create aVisitor, add the visit methods on Expression and its
subclasses, and implement visitors for evaluation, printing, ...
Implementing the Actions
S.Ducasse 52
Visitor Toy Example Solution 1
S.Ducasse 53
Visitor Toy Example 2
S.Ducasse 54
So which solution to take?
In this case you might say:
printing is not easy
adding it directly on Expression clutters Expression (need
to add instance variables etc.)
therefore we can factor out the printing on a separate
class.
if we do this with a visitor we can then implement
evaluation there as well.
Toy Example: Discussion
S.Ducasse 55
Smalltalk has class extensions:
method addition
method replacement
So ‘Decoupling’ actions from items can be done:
e.g., put all the printing methods together.
take care: works only for methods
makes it also really easy to package a visitor!
Note: this is a static solution!
Smalltalk’s class extensions
S.Ducasse 56
Somewhere in the visitor, items are traversed.
Different places where the traversal can be implemented:
in the visitor
on the items hierarchy
Controlling the traversal
S.Ducasse 57
Traversal on theVisitor
S.Ducasse 58
Traversal on the Items
S.Ducasse 59
Sometimes you can pass context information with the
visit methods
So visitors have more information for implementing their
operations
Granularity of Visit Methods
S.Ducasse 60
Regular case: nothing special is going on
Granularity ofVisit Methods
S.Ducasse 61
Here methods allow finer control of variables
(#doTemporaryVariable)
Refined Granularity
S.Ducasse 62
You can implement it as we have shown before.
But notice the general structure of the methods!
This can be taken as advantage:
code can be generated for a visitor.
the method can be performed/invoked
But take care:
only works when there is a full correspondence.
can make the code hard to understand.
Implementation Tricks
S.Ducasse 63
Using #perform:
S.Ducasse 64
Strategy
Define a family of algorithms,
encapsulate each in a separate
class and define each class with
the same interface so that they
can be interchangeable.
Also Know as Policy
S.Ducasse 65
Strategy Intent
• Define a family of algorithms, encapsulate each in a
separate class and define each class with the same
interface so that they can be interchangeable.
S.Ducasse 66
Motivation
Many algorithms exist for breaking a stream into lines.
Hardwiring them into the classes that requires them has
the following problems:
Clients get more complex
Different algorithms can be used at different times
Difficult to add new algorithms at run-time
S.Ducasse 67
Code Smells
Composition>>repair
formatting == #Simple
ifTrue: [ self formatWihtSimpleAlgo]
ifFalse: [ formatting == #Tex
ifTrue: [self formatWithTex]
....]
S.Ducasse 68
Alternative
Composition>>repair
| selector |
selector := (‘formatWith, formatting) asSymbol.
self perform: selector
Still your class gets complex...
S.Ducasse 69
Inheritance?
May not be the solution since:
- you have to create objects of the right class
- it is difficult to change the policy at run-time
- you can get an explosion of classes bloated with the
use of a functionality and the functionalities.
- no clear identification of responsibility
S.Ducasse 70
Strategy Solution
S.Ducasse 71
When
Many related classes differ only in their behavior
You have variants of an algorithm (space/time)
An algorithm uses data that the clients does not have to
know
S.Ducasse 72
Structure
Composition>>repair
formatter format: self
S.Ducasse 73
Participants
Strategy (Compositor)
declares an interface common to all concrete strategies
Concrete Strategies
implement algorithm
Context
configure with concrete strategy
maintains a reference to the concrete strategy
may define an interface to let the strategy access data
S.Ducasse 74
Collaborations (i)
Strategy and Context interact to implement the chosen
algorithm.
A context may pass all data required by the algorithm to
the strategy when the algorithm is called
GraphVisualizer>>graphIt
....
grapher plot: data using: graphPane pen
Grapher>>plot: data using: aPen
S.Ducasse 75
Context passes itself as argument
Also know as self-delegation...
GraphVisualizer>>graphIt
grapher plotFor: self
BartChartGrapher>>plotFor: aGraphVisualizer
|data|
data := aGraphVisualizer data
....
S.Ducasse 76
BackPointer
Grapher class>>for: aGraphVisualizer
^ self new graphVisualizer: aGraphVisualizer
BartChartGrapher>>plot
...
graphVisualizer data..
graphVisualizer pen
Grapher (Strategy) points directly to GraphVisualizer
(Context), so sharing strategy between different context may
be difficult, if sharing is needed then use self-delegation
S.Ducasse 77
Collaboration (ii)
“A context forwards requests from its clients to its
strategy. Clients usually create and pass a
ConcreteStrategy object to the context; thereafter,
clients interact with the context exclusively.“ GOF
Not sure that the client has to choose...
S.Ducasse 78
Consequences
Define a family of pluggable algorithms
Eliminates conditional statements
Clients can choose between several implementations
Clients must be aware of the different strategies
Increase the number of objects
Communication overhead between client and strategies
Weaken encapsulation of the client
S.Ducasse 79
Domain-Specific Objects as Strategies
Strategies do not have to be limited to one single
algorithm
They may represent domain specific knowledge
Mortgage
FixedRateMortgage
OneYear...
S.Ducasse 80
Known Uses
ImageRenderer inVW: “a technique to render an image
using a limited palette”
ImageRenderer
NearestPaint
OrderedDither
ErrorDiffusion
View-Controller
a view instance uses a controller object to handle and
respond to user input via mouse or keyboard.
Controllers can be changed at run-time
S.Ducasse 81
Abstract Factory
Provide an interface for
creating families of related or
dependent objects without
specifying their concrete classes
Also known as: Kit
S.Ducasse 82
Abstract Factory Intent
• Provide an interface for creating families of related or
dependent objects without specifying their concrete
classes
S.Ducasse 83
Abstract Factory Motivation
You have an application with different looks and feels.
How to avoid to hardcode all the specific widget classes
into the code so that you can change from Motifs to
MacOsX?
S.Ducasse 84
Abstract Factory Motivation
Abstract factory introduce an interface for creating each
basic kind of widget
S.Ducasse 85
Abstract Factory Applicability
a system should be independent of how its products are
created, composed, and represented
a system should be configured with one of multiple
families of products
a family of related product objects is designed to be used
together, and you need to enforce this constraint
you want to provide a class library of products, and you
want to reveal just their interfaces, not their
implementations
S.Ducasse 86
Abstract Factory Structure
S.Ducasse 87
Abstract Factory Participants
AbstractFactory (WidgetFactory)
declares an interface for operations that create abstract
product objects
ConcreteFactory (MotifWidgetFactory,
PMWidgetFactory)
implements the operations to create concrete product
objects
S.Ducasse 88
Abstract Factory Participants
AbstractProduct (Window, ScrollBar)
defines a product object to be created by the
corresponding concrete factory
implements the AbstractProduct interface
Client
uses only interfaces declared by AbstractFactory and
AbstractProduct classes
S.Ducasse 89
Implementation: Specifying the
FactoryMazeGame class>>createMazeFactory
^ (MazeFactory new
addPart:Wall named: #wall;
addPart: Room named: #room;
addPart: Door named: #door;
yourself)
EnchantedMazeGame class>>createMazeFactory
^ (MazeFactory new
addPart:Wall named: #wall;
addPart: EnchantedRoom named: #room;
addPart: DoorNeedingSpell named: #door;
yourself)
S.Ducasse 90
SingleFactory
MazeGame class>>createMazeFactory
^ (MazeFactory new
addPart:Wall named: #wall;
addPart: Room named: #room;
addPart: Door named: #door;
yourself)
MazeGame class>>createEnchantedMazeFactory
^ (MazeFactory new
addPart:Wall named: #wall;
addPart: EnchantedRoom named: #room;
addPart: DoorNeedingSpell named: #door;
yourself)
S.Ducasse 91
Implementation: Using the Factory
MazeFactory>>createMaze: aFactory
| room1 room2 aDoor |
room1 := (aFactory make: #room) number: 1.
room2 := (aFactory make: #room) number: 2.
aDoor := (aFactory make: #door) from: room1 to: room2.
room1 atSide: #north put: (aFactory make: #wall).
room1 atSide: #east put: aDoor.
...
room2 atSide: #south put: (aFactory make: #wall).
room2 atSide: #west put: aDoor.
^ Maze new addRoom: room1; addRoom: room2; yourself
MazeFactory>>make: partName
^ (partCatalog at: partName) new
S.Ducasse 92
Abstract Factory Collaborations
Collaborations
Normally a single instance of ConcreteFactory is created
at run-time
AbstractFactory defers creation of product objects to its
ConcreteFactory subclass
S.Ducasse 93
Consequences
It isolates concrete classes
It makes exchanging product families easy
It promotes consistency among products
Supporting new kinds of products is difficult (set of
products is somehow fixed)
The class factory “controls” what is created
S.Ducasse 94
Using Prototypes
The concrete factory stores the prototypes to be cloned in a
dictionary called partCatalog.
make: partName
^ (partCatalog at: partName) copy
The concrete factory has a method for adding parts to the catalog.
addPart: partTemplate named: partName
partCatalog at: partName put: partTemplate
Prototypes are added to the factory by identifying them with a
symbol:
aFactory addPart: aPrototype named: #ACMEWidget
S.Ducasse 95
In Relations
Builder and Abstract Factory are closely related
But Builder is in charge of assembling parts
AbstractFactory is responsible of producing parts that
work together
S.Ducasse 96
Known Uses
VisualWorks UILookPolicy
S.Ducasse 97
Chain of Responsibility
Avoid coupling the sender of
a request to its receiver by
giving more than one object a
chance to handle the request.
Chain the receiving objects and
pass the request along the chain
until an object handles it.
S.Ducasse 98
Chain of Responsibility
Avoid coupling the sender of a request to its receiver by
giving more than one object a chance to handle the
request.
Chain the receiving objects and pass the request along
the chain until an object handles it.
S.Ducasse 99
Motivation
The problem here is that the object that ultimately
provides the help isn't known explicitly to the object
(e.g., the button) that initiates the help request.
How to decouple senders and receivers?
By giving multiple objects a chance to handle a request.
The request gets passed along a chain of objects until
one of them handles it.
S.Ducasse 100
Chain of Resp. Possible Structure
S.Ducasse 101
Participants
Handler
defines an interface for handling requests
may implement the successor link
ConcreteHandler
handles requests it is responsible for
can access its successor
Client
initiates the request to a concreteHandler
S.Ducasse 102
Dynamic
The first object in the chain receives the request and
either handles it or forwards it to the next candidate on
the chain, which does likewise.
The object that made the request has no explicit
knowledge of who will handle it
S.Ducasse 103
Chain
Can be a linked list
But also a tree (cf. Composite Pattern)
Usually order can represent
specific to more general
priority: more important (security... in SmallWiki)
S.Ducasse 104
Consequences (i)
Reduced coupling. The pattern frees an object from
knowing which other object handles a request.
An object only has to know that a request will be handled
"appropriately."
Both the receiver and the sender have no explicit
knowledge of each other, and an object in the chain doesn't
have to know about the chain's structure.
Simplify object interconnections. Instead of objects
maintaining references to all candidate receivers, they keep
a single reference to their successor
S.Ducasse 105
Consequences (II)
Added flexibility in assigning responsibilities to
objects.
flexibility in distributing responsibilities among objects.
can add or change responsibilities for handling a request by
adding to or otherwise changing the chain at run-time.
Receipt isn't guaranteed.
no guarantee it'll be handled: the request can fall off the
end of the chain without ever being handled.
A request can also go unhandled when the chain is not
configured properly.
S.Ducasse 106
Differences with Decorator
A Decorator usually wraps the decorated object: clients
point to the decorator and not the object
A Decorator does not have to forward the same
message
A decorated object does not have to know that it is
wrapped
With a chain of responsibility, the client asks the first
chain objects explicitly.
S.Ducasse 107
Variations
Do the work or pass? or both?
the DP says that the handler either does the work or
passes it to its successor but it can also do part of the
job (see OO recursion)
S.Ducasse 108
OO Recursion: Hash, = and copy
Person>>= aPerson
^ self name = aPerson name
PersonName>>= aPersonName
^ (self firstName = aPersonName firstName)
and: [(self lastName = aPersonName lastName)]
String>>= aString
...
S.Ducasse 109
OO Recursion: Hash, = and copy
Person>>hash
^ self name hash
PersonName>>hash
^ self firstName hash bitXor: self lastName hash
S.Ducasse 110
OO Recursion
With Chain of Responsibility you may recur from leave
to root, from most specific to more general.
Default in root, specific and recursion in leaves
With OO recursion, from composite (person) to
components (leaves)
Default in leave (String =)
S.Ducasse 111
Smalltalk Specific
Automatic Forwarding with doesNotUnderstand:
can work
but can be dangerous
S.Ducasse 112
Wrap-up
Patterns are names
Patterns are about tradeoffs
Know when not to apply them

Weitere ähnliche Inhalte

Andere mochten auch (20)

Stoop 437-proxy
Stoop 437-proxyStoop 437-proxy
Stoop 437-proxy
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 415-design points
Stoop 415-design pointsStoop 415-design points
Stoop 415-design points
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
Stoop 390-instruction stream
Stoop 390-instruction streamStoop 390-instruction stream
Stoop 390-instruction stream
 
06 debugging
06 debugging06 debugging
06 debugging
 
12 - Conditions and Loops
12 - Conditions and Loops12 - Conditions and Loops
12 - Conditions and Loops
 
Stoop 439-decorator
Stoop 439-decoratorStoop 439-decorator
Stoop 439-decorator
 
Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
7 - OOP - OO Concepts
7 - OOP - OO Concepts7 - OOP - OO Concepts
7 - OOP - OO Concepts
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop 430-design patternsintro
Stoop 430-design patternsintroStoop 430-design patternsintro
Stoop 430-design patternsintro
 
Stoop 400-metaclass only
Stoop 400-metaclass onlyStoop 400-metaclass only
Stoop 400-metaclass only
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 

Ähnlich wie Design Patterns Explained in Depth

Ähnlich wie Design Patterns Explained in Depth (20)

1 - OOP
1 - OOP1 - OOP
1 - OOP
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop 304-metaclasses
Stoop 304-metaclassesStoop 304-metaclasses
Stoop 304-metaclasses
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
20080115 04 - La qualimétrie pour comprendre et appréhender les SI
20080115 04 - La qualimétrie pour comprendre et appréhender les SI20080115 04 - La qualimétrie pour comprendre et appréhender les SI
20080115 04 - La qualimétrie pour comprendre et appréhender les SI
 
2 - OOP
2 - OOP2 - OOP
2 - OOP
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Final sdp ppt
Final sdp pptFinal sdp ppt
Final sdp ppt
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Design Patterns - GOF
Design Patterns - GOFDesign Patterns - GOF
Design Patterns - GOF
 
L03 Design Patterns
L03 Design PatternsL03 Design Patterns
L03 Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 
L05 Design Patterns
L05 Design PatternsL05 Design Patterns
L05 Design Patterns
 

Mehr von The World of Smalltalk (17)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
10 reflection
10 reflection10 reflection
10 reflection
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
02 basics
02 basics02 basics
02 basics
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 

Kürzlich hochgeladen

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Kürzlich hochgeladen (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Design Patterns Explained in Depth

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ Selected Design Patterns
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. S.Ducasse 3 Goal • What are patterns? • Why? • Patterns are not god on earth • Example
  • 4. S.Ducasse 4 Design Patterns • Design patterns are recurrent solutions to design problems • They are names • Composite,Visitor, Observer... • They are pros and cons
  • 5. S.Ducasse 5 Christoffer Alexander “The Timeless Way of Building”, Christoffer Alexander, Oxford University Press, 1979, ISBN 0195024028 More advanced than what is used in computer science only the simple parts got used. pattern languages were skipped. From Architecture
  • 6. S.Ducasse 6 Why Patterns? Smart Elegant solutions that a novice would not think of Generic Independent on specific system type, language Well-proven Successfully tested in several systems Simple Combine them for more complex solutions There are really stupid patterns (supersuper) in some books so watch out!!!
  • 7. S.Ducasse 7 Reusable solutions to common problems based on experiences from real systems Names of abstractions above class and object level a common vocabulary for developers Handling of functional and non-functional aspects separating interfaces/implementation, loose coupling between parts, … A basis for frameworks and toolkits basic constructs to improve reuse Education and training support Patterns provide...
  • 8. S.Ducasse 8 Pattern name Increase of design vocabulary Problem description When to apply it, in what context to use it Solution description (generic !) The elements that make up the design, their relationships, responsibilities, and collaborations Consequences Results and trade-offs of applying the pattern Elements in a Pattern
  • 9. S.Ducasse 9 Composite • Compose objects into tree structures to represent part- whole hierarchies. • Composite lets clients treat individual objects and compositions of objects uniformly
  • 10. S.Ducasse 10 Composite Intent Intent: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly
  • 12. S.Ducasse 12 Composite Pattern Applicability Use the Composite Pattern when : you want to represent part-whole hierarchies of objects you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly
  • 14. S.Ducasse 14 Composite Pattern Participants Component (Graphic) declares the interface for objects in the composition implements default behavior for the interface common to all classes, as appropriate declares an interface for accessing and managing its child components Leaf (Rectangle, Line,Text, ...) represents leaf objects in the composition. A leaf has no children defines behavior for primitive objects in the composition
  • 15. S.Ducasse 15 Composite Pattern Composite (Picture) defines behaviour for components having children stores child components implements child-related operations in the Component interface Client manipulates objects in the composition through the Component interface
  • 16. S.Ducasse 16 Composite Pattern Collaborations Clients use the Component class interface to interact with objects in the composite structure. Leaves handle requests directly. Composites forward requests to its child components Consequences defines class hierarchies consisting of primitive and composite objects. Makes the client simple. Composite and primitive objects are treated uniformly. (no cases) Eases the creation of new kinds of components Can make your design overly general
  • 17. S.Ducasse 17 An Alternate Structure Again structure is not intent!
  • 18. S.Ducasse 18 Queries... • To be able to specify different queries over a repository q1 := PropertyQuery property: #HNL with: #< value: 4. q2 := PropertyQuery property: #NOM with: #> value: 10. q3 := MatchName match:‘*figure*’ • Compose these queries and treat composite queries as one query • (e1 e2 e3 e4 ... en)((q1 and q2 and q4) or q3) -> (e2 e5) • composer := AndComposeQuery with: (Array with: q1 with: q2 with: q3)
  • 20. S.Ducasse 20 In Smalltalk • Composite not only groups leaves but can also contain composites • In Smalltalk add:, remove: do not need to be declared into Component but only on Composite.This way we avoid to have to define dummy behavior for Leaf
  • 21. S.Ducasse 21 CompositeVariations • Use a Component superclass to define the interface and factor code there. • Consider implementing abstract Composite and Leaf (in case of complex hierarchy) • Only Composite delegates to children • Composites can be nested • Composite sets the parent back-pointer (add:/remove:)
  • 22. S.Ducasse 22 CompositeVariations • Can Composite contain any type of child? (domain issues) • Is the Composite’s number of children limited? • Forward – Simple forward. Send the message to all the children and merge the results without performing any other behavior – Selective forward. Conditionally forward to some children – Extended forward. Extra behavior – Override. Instead of delegating
  • 23. S.Ducasse 23 Other Patterns • Composite andVisitors • Visitors walks on structured objects • Composite and Factories • Factories can create composite elements
  • 25. S.Ducasse 25 Creational Patterns Instantiation and configuration of classes and objects Structural Patterns Usage of classes and objects in larger structures, separation of interfaces and implementation Behavioral Patterns Algorithms and division of responsibility Concurrency Distribution Security Categories of Design Patterns
  • 26. S.Ducasse 26 Some Creational Patterns Abstract factory Builder Factory Method Prototype Singleton
  • 27. S.Ducasse 27 Some Structural Patterns Adapter Bridge Composite Decorator Façade Flyweight Proxy
  • 28. S.Ducasse 28 Some Behavioral Patterns Chain of responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor
  • 29. S.Ducasse 29 Alert!!! Patterns are invading • Design Patterns may be a real plague! • Do not apply them when you do not need them • Design Patterns make the software more complex – More classes – More indirections, more messages • Try to understand when NOT applying them!
  • 30. S.Ducasse 30 About Pattern Implementation This is POSSIBLE implementation not a definitive one Do not confuse structure and intent!!! Patterns are about INTENT and TRADEOFFS
  • 31. S.Ducasse 31 Singleton Ensure that a class has only one instance, and provide a global point of access to it
  • 32. S.Ducasse 32 The Singleton Pattern • Intent: Ensure that a class has only one instance, and provide a global point of access to it • Problem: We want a class with a unique instance. • Solution: We specialize the #new class method so that if one instance already exists this will be the only one. When the first instance is created, we store and return it as result of #new.
  • 34. S.Ducasse 34 The Singleton Pattern |aLan| aLan := NetworkManager new aLan == LAN new -> true aLan uniqueInstance == NetworkManager new -> true NetWorkManager class instanceVariableNames: 'uniqueInstance ' NetworkManager class>>new self error:‘should use uniqueInstance’ NetworkManager class>>uniqueInstance uniqueInstance isNil ifTrue: [ uniqueInstance := self basicNew initialize]. ^uniqueInstance
  • 35. S.Ducasse 35 The Singleton Pattern • Providing access to the unique instance is not always necessary. • It depends on what we want to express.The difference between #new and #uniqueInstance is that #new potentially initializes a new instance, while #uniqueInstance only returns the unique instance (there is no initialization) • Do we want to communicate that the class has a singleton? new? defaultInstance? uniqueInstance?
  • 36. S.Ducasse 36 Implementation Issues • Singletons may be accessed via a global variable (ex: NotificationManager uniqueInstance notifier). SessionModel>>startupWindowSystem “Private - Perform OS window system startup” Notifier initializeWindowHandles. ... oldWindows := Notifier windows. Notifier initialize. ... ^oldWindows • GlobalVariable or Class Method Access –GlobalVariable Access is dangerous: if we reassign Notifier we lose all references to the current window. –Class Method Access is better because it provides a single access point. This class is responsible for the singleton instance (creation, initialization,...).
  • 37. S.Ducasse 37 Implementation Issues Persistent Singleton: only one instance exists and its identity does not change (ex: NotifierManager inVisual Smalltalk) Transient Singleton: only one instance exists at any time, but that instance changes (ex: SessionModel in Visual Smalltalk, SourceFileManager, Screen in VisualWorks) Single Active Instance Singleton: a single instance is active at any point in time, but other dormant instances may also exist. Project inVisualWorks
  • 38. S.Ducasse 38 Implementation Issues classVariable or class instance variable classVariable One singleton for a complete hierarchy Class instance variable One singleton per class
  • 39. S.Ducasse 39 Access? In Smalltalk we cannot prevent a client to send a message (protected in C++).To prevent additional creation we can redefine new/new: Object subclass: #Singleton instanceVariableNames:‘uniqueInstance’ classVariableNames:‘’ poolDictionaries:‘’ Singleton class>>new self error:‘Class ‘, self name,‘ cannot create new instances’
  • 40. S.Ducasse 40 Access using new: not good idea Singleton class>>new ^self uniqueInstance The intent (uniqueness) is not clear anymore! New is normally used to return newly created instances.The programmer does not expect this: |screen1 screen2| screen1 := Screen new. screen2 := Screen uniqueInstance
  • 41. S.Ducasse 41 Favor Instance Behavior When a class should only have one instance, it could be tempting to define all its behavior at the class level. But this is not good: Class behavior represents behavior of classes:“Ordinary objects are used to model the real world. MetaObjects describe these ordinary objects” Do not mess up this separation and do not mix domain objects with metaconcerns. What’s happens if later on an object can have multiple instances?You have to change a lot of client code!
  • 42. S.Ducasse 42 Time and not Scope Singleton is about time not access time: only one instance is available at the same time access: can’t you add an instance to refer to the object? Singleton for access are as bad as global variables Often we can avoid singleton by passing/referring to the object instead of favoring a global access point It is worth to have one extra instance variable that refers to the right object
  • 43. S.Ducasse 43 Visitor Represent an operation to be performed on the elements of an object structure in a class separate from the elements themselves.Visitor lets you define a new operation without changing the classes of the elements on which it operates.
  • 44. S.Ducasse 44 Intent: Represent an operation to be performed on the elements of an object structure in a class separate from the elements themselves.Visitor lets you define a new operation without changing the classes of the elements on which it operates. Visitor Intent
  • 46. S.Ducasse 46 Whenever you have a number of items on which you have to perform a number of actions, and When you ‘decouple’ the actions from the items. Examples: the parse tree (ProgramNode) uses a visitor for the compilation (emitting code on CodeStream) GraphicsContext is a visitor forVisualComponents, Geometrics, and some other ones (CharacterArray, ...) Rendering documents When to use aVisitor
  • 47. S.Ducasse 47 So all our problems are solved, no? Well... when to use a visitor control over item traversal choosing the granularity of visitor methods implementation tricks Applying theVisitor
  • 48. S.Ducasse 48 Use aVisitor: when the operations on items change a lot. Do not use a visitor: when the items you want to visit change a lot. Question: But how do we know what to choose up- front? When to Use aVisitor
  • 49. S.Ducasse 49 Language to deal with arithmetic expressions. It supports one kind of number, and has +, *, (, ) We want to evaluate expressions, and print them. Example: 1 + 1 result: 1 + 1 = 2 ((4 * 2) * (3 + 5)) * 3 result: (4 * 2 * (3 + 5)) * 3 = 192 ... Visitor Toy Example
  • 50. S.Ducasse 50 Visitor Toy Example: ParseTree
  • 51. S.Ducasse 51 Two solutions: add methods for evaluating, printing, ... on Expression and its subclasses create aVisitor, add the visit methods on Expression and its subclasses, and implement visitors for evaluation, printing, ... Implementing the Actions
  • 52. S.Ducasse 52 Visitor Toy Example Solution 1
  • 54. S.Ducasse 54 So which solution to take? In this case you might say: printing is not easy adding it directly on Expression clutters Expression (need to add instance variables etc.) therefore we can factor out the printing on a separate class. if we do this with a visitor we can then implement evaluation there as well. Toy Example: Discussion
  • 55. S.Ducasse 55 Smalltalk has class extensions: method addition method replacement So ‘Decoupling’ actions from items can be done: e.g., put all the printing methods together. take care: works only for methods makes it also really easy to package a visitor! Note: this is a static solution! Smalltalk’s class extensions
  • 56. S.Ducasse 56 Somewhere in the visitor, items are traversed. Different places where the traversal can be implemented: in the visitor on the items hierarchy Controlling the traversal
  • 59. S.Ducasse 59 Sometimes you can pass context information with the visit methods So visitors have more information for implementing their operations Granularity of Visit Methods
  • 60. S.Ducasse 60 Regular case: nothing special is going on Granularity ofVisit Methods
  • 61. S.Ducasse 61 Here methods allow finer control of variables (#doTemporaryVariable) Refined Granularity
  • 62. S.Ducasse 62 You can implement it as we have shown before. But notice the general structure of the methods! This can be taken as advantage: code can be generated for a visitor. the method can be performed/invoked But take care: only works when there is a full correspondence. can make the code hard to understand. Implementation Tricks
  • 64. S.Ducasse 64 Strategy Define a family of algorithms, encapsulate each in a separate class and define each class with the same interface so that they can be interchangeable. Also Know as Policy
  • 65. S.Ducasse 65 Strategy Intent • Define a family of algorithms, encapsulate each in a separate class and define each class with the same interface so that they can be interchangeable.
  • 66. S.Ducasse 66 Motivation Many algorithms exist for breaking a stream into lines. Hardwiring them into the classes that requires them has the following problems: Clients get more complex Different algorithms can be used at different times Difficult to add new algorithms at run-time
  • 67. S.Ducasse 67 Code Smells Composition>>repair formatting == #Simple ifTrue: [ self formatWihtSimpleAlgo] ifFalse: [ formatting == #Tex ifTrue: [self formatWithTex] ....]
  • 68. S.Ducasse 68 Alternative Composition>>repair | selector | selector := (‘formatWith, formatting) asSymbol. self perform: selector Still your class gets complex...
  • 69. S.Ducasse 69 Inheritance? May not be the solution since: - you have to create objects of the right class - it is difficult to change the policy at run-time - you can get an explosion of classes bloated with the use of a functionality and the functionalities. - no clear identification of responsibility
  • 71. S.Ducasse 71 When Many related classes differ only in their behavior You have variants of an algorithm (space/time) An algorithm uses data that the clients does not have to know
  • 73. S.Ducasse 73 Participants Strategy (Compositor) declares an interface common to all concrete strategies Concrete Strategies implement algorithm Context configure with concrete strategy maintains a reference to the concrete strategy may define an interface to let the strategy access data
  • 74. S.Ducasse 74 Collaborations (i) Strategy and Context interact to implement the chosen algorithm. A context may pass all data required by the algorithm to the strategy when the algorithm is called GraphVisualizer>>graphIt .... grapher plot: data using: graphPane pen Grapher>>plot: data using: aPen
  • 75. S.Ducasse 75 Context passes itself as argument Also know as self-delegation... GraphVisualizer>>graphIt grapher plotFor: self BartChartGrapher>>plotFor: aGraphVisualizer |data| data := aGraphVisualizer data ....
  • 76. S.Ducasse 76 BackPointer Grapher class>>for: aGraphVisualizer ^ self new graphVisualizer: aGraphVisualizer BartChartGrapher>>plot ... graphVisualizer data.. graphVisualizer pen Grapher (Strategy) points directly to GraphVisualizer (Context), so sharing strategy between different context may be difficult, if sharing is needed then use self-delegation
  • 77. S.Ducasse 77 Collaboration (ii) “A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively.“ GOF Not sure that the client has to choose...
  • 78. S.Ducasse 78 Consequences Define a family of pluggable algorithms Eliminates conditional statements Clients can choose between several implementations Clients must be aware of the different strategies Increase the number of objects Communication overhead between client and strategies Weaken encapsulation of the client
  • 79. S.Ducasse 79 Domain-Specific Objects as Strategies Strategies do not have to be limited to one single algorithm They may represent domain specific knowledge Mortgage FixedRateMortgage OneYear...
  • 80. S.Ducasse 80 Known Uses ImageRenderer inVW: “a technique to render an image using a limited palette” ImageRenderer NearestPaint OrderedDither ErrorDiffusion View-Controller a view instance uses a controller object to handle and respond to user input via mouse or keyboard. Controllers can be changed at run-time
  • 81. S.Ducasse 81 Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes Also known as: Kit
  • 82. S.Ducasse 82 Abstract Factory Intent • Provide an interface for creating families of related or dependent objects without specifying their concrete classes
  • 83. S.Ducasse 83 Abstract Factory Motivation You have an application with different looks and feels. How to avoid to hardcode all the specific widget classes into the code so that you can change from Motifs to MacOsX?
  • 84. S.Ducasse 84 Abstract Factory Motivation Abstract factory introduce an interface for creating each basic kind of widget
  • 85. S.Ducasse 85 Abstract Factory Applicability a system should be independent of how its products are created, composed, and represented a system should be configured with one of multiple families of products a family of related product objects is designed to be used together, and you need to enforce this constraint you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
  • 87. S.Ducasse 87 Abstract Factory Participants AbstractFactory (WidgetFactory) declares an interface for operations that create abstract product objects ConcreteFactory (MotifWidgetFactory, PMWidgetFactory) implements the operations to create concrete product objects
  • 88. S.Ducasse 88 Abstract Factory Participants AbstractProduct (Window, ScrollBar) defines a product object to be created by the corresponding concrete factory implements the AbstractProduct interface Client uses only interfaces declared by AbstractFactory and AbstractProduct classes
  • 89. S.Ducasse 89 Implementation: Specifying the FactoryMazeGame class>>createMazeFactory ^ (MazeFactory new addPart:Wall named: #wall; addPart: Room named: #room; addPart: Door named: #door; yourself) EnchantedMazeGame class>>createMazeFactory ^ (MazeFactory new addPart:Wall named: #wall; addPart: EnchantedRoom named: #room; addPart: DoorNeedingSpell named: #door; yourself)
  • 90. S.Ducasse 90 SingleFactory MazeGame class>>createMazeFactory ^ (MazeFactory new addPart:Wall named: #wall; addPart: Room named: #room; addPart: Door named: #door; yourself) MazeGame class>>createEnchantedMazeFactory ^ (MazeFactory new addPart:Wall named: #wall; addPart: EnchantedRoom named: #room; addPart: DoorNeedingSpell named: #door; yourself)
  • 91. S.Ducasse 91 Implementation: Using the Factory MazeFactory>>createMaze: aFactory | room1 room2 aDoor | room1 := (aFactory make: #room) number: 1. room2 := (aFactory make: #room) number: 2. aDoor := (aFactory make: #door) from: room1 to: room2. room1 atSide: #north put: (aFactory make: #wall). room1 atSide: #east put: aDoor. ... room2 atSide: #south put: (aFactory make: #wall). room2 atSide: #west put: aDoor. ^ Maze new addRoom: room1; addRoom: room2; yourself MazeFactory>>make: partName ^ (partCatalog at: partName) new
  • 92. S.Ducasse 92 Abstract Factory Collaborations Collaborations Normally a single instance of ConcreteFactory is created at run-time AbstractFactory defers creation of product objects to its ConcreteFactory subclass
  • 93. S.Ducasse 93 Consequences It isolates concrete classes It makes exchanging product families easy It promotes consistency among products Supporting new kinds of products is difficult (set of products is somehow fixed) The class factory “controls” what is created
  • 94. S.Ducasse 94 Using Prototypes The concrete factory stores the prototypes to be cloned in a dictionary called partCatalog. make: partName ^ (partCatalog at: partName) copy The concrete factory has a method for adding parts to the catalog. addPart: partTemplate named: partName partCatalog at: partName put: partTemplate Prototypes are added to the factory by identifying them with a symbol: aFactory addPart: aPrototype named: #ACMEWidget
  • 95. S.Ducasse 95 In Relations Builder and Abstract Factory are closely related But Builder is in charge of assembling parts AbstractFactory is responsible of producing parts that work together
  • 97. S.Ducasse 97 Chain of Responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
  • 98. S.Ducasse 98 Chain of Responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
  • 99. S.Ducasse 99 Motivation The problem here is that the object that ultimately provides the help isn't known explicitly to the object (e.g., the button) that initiates the help request. How to decouple senders and receivers? By giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it.
  • 100. S.Ducasse 100 Chain of Resp. Possible Structure
  • 101. S.Ducasse 101 Participants Handler defines an interface for handling requests may implement the successor link ConcreteHandler handles requests it is responsible for can access its successor Client initiates the request to a concreteHandler
  • 102. S.Ducasse 102 Dynamic The first object in the chain receives the request and either handles it or forwards it to the next candidate on the chain, which does likewise. The object that made the request has no explicit knowledge of who will handle it
  • 103. S.Ducasse 103 Chain Can be a linked list But also a tree (cf. Composite Pattern) Usually order can represent specific to more general priority: more important (security... in SmallWiki)
  • 104. S.Ducasse 104 Consequences (i) Reduced coupling. The pattern frees an object from knowing which other object handles a request. An object only has to know that a request will be handled "appropriately." Both the receiver and the sender have no explicit knowledge of each other, and an object in the chain doesn't have to know about the chain's structure. Simplify object interconnections. Instead of objects maintaining references to all candidate receivers, they keep a single reference to their successor
  • 105. S.Ducasse 105 Consequences (II) Added flexibility in assigning responsibilities to objects. flexibility in distributing responsibilities among objects. can add or change responsibilities for handling a request by adding to or otherwise changing the chain at run-time. Receipt isn't guaranteed. no guarantee it'll be handled: the request can fall off the end of the chain without ever being handled. A request can also go unhandled when the chain is not configured properly.
  • 106. S.Ducasse 106 Differences with Decorator A Decorator usually wraps the decorated object: clients point to the decorator and not the object A Decorator does not have to forward the same message A decorated object does not have to know that it is wrapped With a chain of responsibility, the client asks the first chain objects explicitly.
  • 107. S.Ducasse 107 Variations Do the work or pass? or both? the DP says that the handler either does the work or passes it to its successor but it can also do part of the job (see OO recursion)
  • 108. S.Ducasse 108 OO Recursion: Hash, = and copy Person>>= aPerson ^ self name = aPerson name PersonName>>= aPersonName ^ (self firstName = aPersonName firstName) and: [(self lastName = aPersonName lastName)] String>>= aString ...
  • 109. S.Ducasse 109 OO Recursion: Hash, = and copy Person>>hash ^ self name hash PersonName>>hash ^ self firstName hash bitXor: self lastName hash
  • 110. S.Ducasse 110 OO Recursion With Chain of Responsibility you may recur from leave to root, from most specific to more general. Default in root, specific and recursion in leaves With OO recursion, from composite (person) to components (leaves) Default in leave (String =)
  • 111. S.Ducasse 111 Smalltalk Specific Automatic Forwarding with doesNotUnderstand: can work but can be dangerous
  • 112. S.Ducasse 112 Wrap-up Patterns are names Patterns are about tradeoffs Know when not to apply them