Professional Documents
Culture Documents
Design Patterns
Design Patterns
UML Diagrams
Creational Patterns
Structural Patterns
Main source:
Design Patterns: Elements of Reusable Object
Oriented Software. Gamma, E., Helm, R.,
Johnson, R. and Vlissides, J. (so-called Gang
of Four)
UML: Class Diagram
ClassName top: class name
dataMember1 : type1
dataMember2 : type2 middle: data members
method1( parameter : type3 )
bottom: methods
ClassName
visibility is denoted by a
# dataMember1 : type1 prefix:
- dataMember2 : type2 = 0
+ method1( parameter : type3 )
+ for public
# for protected
- for private
UML: Generalisation (or inheritance)
AbstractShape Pure virtual methods
# position : int[2]
# color : char[3] are written in italic
+ draw( display : BaseDisplay & )
Inheritance is denoted
with an hollow
triangle pointing at
CircleShape the parent class.
- radius : int
- definition
+ draw( display : BaseDisplay & ) + draw( display : BaseDisplay & )
class
class MyFunc
MyFunc {{
public:
public:
double
double operator()(double);
operator()(double);
};
};
double
double compute_sum(
compute_sum( const
const MyFunc
MyFunc &,
&, double
double lo,
lo, double
double hi);
hi);
void
void main(void){
main(void){
cout
cout <<
<< compute_sum(
compute_sum( MyFunc(),
MyFunc(), 0.0,
0.0, 1.0
1.0 )) <<
<< endl;
endl;
}}
Function Objects (Functors)
Advantage: you can control the allowed functions
using inheritance:
MyFunc
+ operator()( x : double )
SqrFunc PowerFunc
- p : double
class
class SqrFunc
SqrFunc :: public
public MyFunc
MyFunc {{
public:
Advantage: you can control the allowed functions
public:
double
double operator()(double
operator()(double x)x) const
const {{
using inheritance:
}}
return
return x*x;
x*x;
};
};
class
class PowerFunc
PowerFunc :: public
public MyFunc
MyFunc {{
double
double p;
p;
public:
public:
PowerFunc(double
PowerFunc(double power)
power) :: p(power)
p(power) {}{}
double
double operator()(double
operator()(double x)x) const
const {{
return
return pow(x,p);
pow(x,p);
}}
};
};
double
double compute_sum(
compute_sum( const
const MyFunc
MyFunc &f,
&f, double
double lo,
lo, double
double hi);
hi);
int
int main(void){
main(void){
cout
cout <<
<< compute_sum(
compute_sum( SqrFunc(),
SqrFunc(), 0.0,
0.0, 1.0
1.0 )) <<
<< endl;
endl;
cout
cout <<
<< compute_sum(
compute_sum( PowerFunc(3.0),
PowerFunc(3.0), 0.0,
0.0, 1.0
1.0 )) <<
<< endl;
endl;
}}
Function Objects
Allow to restrict parameters to a family of
functions
Allow to pass extra parameters to the function in
the object constructor
You can use either function pointer or function
objects with the STL algo. (eg, sort, for_each).
Creational Patterns
Creational Patterns
Problem: Your code need to handle different
implementation of an abstract class, depending
on context. How do you create these objects
on the fly?
+ createMario() + createMario()
+ createLuigi() + draw() + draw()
+ createLuigi()
Abstract Factory
Symmetry of
hierarchies between
Factory & Products
Easy to switch
product families (here
from *1 to *2)
Consistency amongst
products.
+ open()
+ close() - documentsOpen
+ save() + createDocument()
+ newDocument() Document *doc;
doc = createDocument();
doc->open();
DOCX MS Word
creates
+
createDocume
nt()
Factory Method
CircleShape
- radius : int
+ draw( display : BaseDisplay & )
+ clone() : AbstractShape *
Prototype
drawback: all
subclasses need to
implement clone()
Structural Patterns
Structural Patterns
How class & objects are composed to form
larger structure.
Simple example would be, eg, multiple
inheritance.
Patterns discussed:
Adapter how to integrate disparate designs
Composite recurrence.
(Decorator allow to add functionalities on the fly.)
Adapter
Motivation: You want to integrate an object in
your code, but its interface is incompatible. An
adapter class works as a translator between
the two designs.
Typical example: you need to do some
complicated video encoding, and you have
found a library that does it, but the interface for
their video objects is different from your
program...
Adapter
A) Class Adapter
B) Object Adapter
+ open() + OpenVideo()
+ close() + CloseVideo()
+ play() + PlayVideo()
void play() {
PlayFile();
}
Adapter
CircleShape ComplexShape 1
- radius : int
+ draw( display : BaseDisplay & ) - shapes
+ clone() : AbstractShape * + draw( display : BaseDisplay & )
+ clone() : AbstractShape *
Composite
Defines class hierarchy that
contains simple and
composite objects.
Makes the client simple:
can handle all objects the
same way.
Can add new components
on the fly easily.
Decorator
+ display()
AbstractText T = new TextColor( red, new TextFont( Arial, new RawText( Hello World ) ) );
T.display();
Hello world
Decorator
More flexible than static
inheritance: run-time!
Can add functionalities when
needed allow for simple
abstract classes.
Can be tricky to understand
and to maintain (document
your code!)
Summary
Design patterns form a useful toolbox.
Each pattern offer a solution for a design problem
can allow fast design of complex solutions.
Patterns should be adapted to the problem, and
simplified wherever possible:
a simpler design is generally a better design!
There are more, check the book!