This is part of my course on Object Oriented Programming. It talks about design patterns, the famous GOF patterns, and how to select the right design pattern.
Software Design Patterns - Selecting the right design pattern
1. Selecting Design PatternsSelecting Design Patterns
João Miguel PereiraJoão Miguel Pereira
http://jpereira.eu
May 2012
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution‐NonCommercial‐ShareAlike 3.0
Unported License.
2. Selecting Design PatternsSelecting Design Patterns
• Pattern IntentPattern Intent
• Pattern Purpose & Scope
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
2
3. Selecting Design PatternsSelecting Design Patterns
• Pattern IntentPattern Intent
• Pattern Purpose & Scope
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
3
4. Pattern IntentPattern Intent
Intent Pattern
Convert the interface of a class into AdapterConvert the interface of a class into
another interface clients expect. Adapter
lets classes work together that couldn't
otherwise because of incompatible
Adapter
interfaces
Decouple an abstraction from its
implementation so that the two can vary
i d d tl
Bridge
independently
Separate the construction of a complex
object from its representation so that the
same construction process can create
Builder
same construction process can create
different representations.
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
4
5. Pattern IntentPattern Intent
Intent Pattern
Avoid coupling the sender of a request to Chain of ResponsibilityAvoid 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
Chain of Responsibility
request along the chain until an object
handles it.
Encapsulate a request as an object,
th b l tti t i li t
Command
thereby letting you parameterize clients
with different requests, queue or log
requests, and support undoable
operations.
Compose objects into tree structures to
represent part‐whole hierarchies.
Composite lets clients treat individual
Composite
objects and compositions of objects
uniformly.
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
5
6. Pattern IntentPattern Intent
Intent Pattern
Attach additional responsibilities to an DecoratorAttach additional responsibilities to an
object dynamically. Decorators provide a
flexible alternative to subclassing for
extending functionality.
Decorator
Provide a unified interface to a set of
interfaces in a subsystem. Facade defines
a higher‐level interface that makes the
b t i t
Façade
subsystem easier to use.
Define an interface for creating an object,
but let subclasses decide which class to
instantiate Factory Method lets a class
Factory Method
instantiate. Factory Method lets a class
defer instantiation to subclasses.
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
6
7. Pattern IntentPattern Intent
Intent Pattern
Use sharing to support large numbers of FlyweightUse sharing to support large numbers of
fine‐grained objects efficiently.
Flyweight
Given a language, define a representation
for its grammar along with an interpreter
Interpreter
g g p
that uses the representation to interpret
sentences in the language.
Provide a way to access the elements of Iterator
an aggregate object sequentially without
exposing its underlying representation.
Define an object that encapsulates how a
f bj i M di
Mediator
set of objects interact. Mediator
promotes loose coupling by keeping
objects from referring to each other
explicitly, and it lets you vary their p y, y y
interaction independently.
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
7
8. Pattern IntentPattern Intent
Intent Pattern
Without violating encapsulation capture MementoWithout violating encapsulation, capture
and externalize an object's internal state
so that the object can be restored to this
state later.
Memento
Define a one‐to‐many dependency
between objects so that when one object
changes state, all its dependents are
tifi d d d t d t ti ll
Observer
notified and updated automatically.
Specify the kinds of objects to create
using a prototypical instance, and create
new objects by copying this prototype
Prototype
new objects by copying this prototype.
Provide a surrogate or placeholder for
another object to control access to it.
Proxy
Ensure a class only has one instance and SingletonEnsure a class only has one instance, and
provide a global point of access to it.
Singleton
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
8
9. Pattern IntentPattern Intent
Intent Pattern
Allow an object to alter its behavior when StateAllow an object to alter its behavior when
its internal state changes. The object will
appear to change its class.
State
Define a family of algorithms, encapsulate Strategyy g , p
each one, and make them
interchangeable. Strategy lets the
algorithm vary independently from clients
th t it
gy
that use it.
Define the skeleton of an algorithm in an
operation, deferring some steps to
subclasses Template Method lets
Template Method
subclasses. Template Method lets
subclasses redefine certain steps of an
algorithm without changing the
algorithm's structure.
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
9
10. Pattern IntentPattern Intent
Intent Pattern
Represent an operation to be performed VisitorRepresent an operation to be performed
on the elements of an object structure.
Visitor lets you define a new operation
without changing the classes of the
Visitor
elements on which it operates.
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
10
11. Selecting Design PatternsSelecting Design Patterns
• Pattern IntentPattern Intent
• Pattern Purpose & Scope
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
11
12. Pattern PurposePattern Purpose
Purpose Classification
The process of object
creation
Creational
The composition of classes
or objects
Structural
The way in which classes
or objects interact and
distribute responsibility
Behavioral
distribute responsibility
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
12
13. Pattern ScopePattern Scope
Scope What does it means
Class Affect relationships between classes and
their subclasses, generally through
inheritance. Cannot be changed in
runtime since the hierarchy is static‐fixedruntime, since the hierarchy is static‐fixed
at compile time.
Object Affect relationship between objects,
which can be changed at run‐time and are g
more dynamic.
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
13
14. Classification by Purpose and ScopeClassification by Purpose and Scope
Purpose
Creational Structural Behavioral
Class •Factory Method •Adapter •Interpreter
•Template Method
Object •Abstract Factory
•Builder
•Prototype
•Bridge
•Composite
•Chain of Responsibility
•Command
•Iterator
SS
•Singleton •Decorator
•Facade
•Flyweight
•Proxy
•Mediator
•Memento
•Observer
•State
c
o
p
e
c
o
p
e
•Strategy
•Visitor
Selecting Design Patterns by João Miguel Pereira is licensed under a Creative Commons
Attribution‐NonCommercial‐ShareAlike 3.0 Unported License.
14