An Introduction to Design
Patterns
Fabrizio Maria Maggi
Institute of Computer Science(The java code and the material is taken from: https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm)
1 Systems modelling – Fabrizio Maria Maggi
Software Development Methodology
Domain (Class)
Model Interaction
Modelling
Application (Class)
Model
Code
Domain Classes;
Application Classes (e.g.,
Patterns); Attributes;
Relations; Operations
2 Systems modelling – Fabrizio Maria Maggi
Application (Class) Model
3 Systems modelling – Fabrizio Maria Maggi
Application (Class) Model
4 Systems modelling – Fabrizio Maria Maggi
Application (Class) Model
5 Systems modelling – Fabrizio Maria Maggi
Application (Class) Model
6
What is Gang of Four (GOF)
� Gamma, Helm, Johnson, Vlissides. Design Patterns -
Elements of Reusable Object-Oriented Software
Addison-Wesley 1994
� Erich Gamma, Richard Helm, Ralph Johnson and John
Vlissides initiated the concept of Design Pattern in
Software development
� These authors are collectively known as Gang of Four
(GOF).
7
Design Patterns
� Design patterns represent the best practices used by
experienced object-oriented software developers
� Design patterns are solutions to general problems that
software developers faced during software development
� These solutions were obtained by trial and error by
numerous software developers over quite a substantial
period of time
� Learning these patterns helps unexperienced developers
to learn software design in an easy and faster way
8
Categories of Design Patterns
9
Categories of Design Patterns
10 Systems modelling – Fabrizio Maria Maggi
Factory
� This type of design pattern comes under creational
pattern
� In Factory pattern, we create object without exposing the
creation logic to the client and refer to newly created
object using a common interface
11 Systems modelling – Fabrizio Maria Maggi
Factory
12 Systems modelling – Fabrizio Maria Maggi
Factory
13 Systems modelling – Fabrizio Maria Maggi
Factory
14 Systems modelling – Fabrizio Maria Maggi
Factory
15 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
� Abstract Factory patterns work around a super-factory
which creates other factories. This factory is also called as
factory of factories
� This type of design pattern comes under creational
pattern
� In Abstract Factory pattern an interface is responsible for
creating a factory of related objects without explicitly
specifying their classes. Each generated factory can give the
objects as per the Factory pattern
16 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
17 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
18 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
19 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
20 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
21 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
22 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
23 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
24 Systems modelling – Fabrizio Maria Maggi
Abstract Factory
25
Abstract Factory
26 Systems modelling – Fabrizio Maria Maggi
Singleton
� Singleton pattern is one of the simplest design patterns in
Java
� This type of design pattern comes under creational
pattern
� This pattern involves a single class which is responsible to
create an object while making sure that only single object
gets created. This class provides a way to access its only
object which can be accessed directly without need to
instantiate the object of the class
27 Systems modelling – Fabrizio Maria Maggi
Singleton
28 Systems modelling – Fabrizio Maria Maggi
Singleton
29 Systems modelling – Fabrizio Maria Maggi
Builder
� Builder pattern builds a complex object using simple
objects and using a step by step approach
� This type of design pattern comes under creational
pattern
� A Builder class builds the final object step by step. This
builder is independent of other objects
30 Systems modelling – Fabrizio Maria Maggi
Builder
31 Systems modelling – Fabrizio Maria Maggi
Builder
32 Systems modelling – Fabrizio Maria Maggi
Builder
33 Systems modelling – Fabrizio Maria Maggi
Builder
34 Systems modelling – Fabrizio Maria Maggi
Builder
35 Systems modelling – Fabrizio Maria Maggi
Builder
36 Systems modelling – Fabrizio Maria Maggi
Builder
37 Systems modelling – Fabrizio Maria Maggi
Builder
38
Categories of Design Patterns
39 Systems modelling – Fabrizio Maria Maggi
Adapter
� Adapter pattern works as a bridge between two
incompatible interfaces
� This type of design pattern comes under structural
pattern as this pattern combines the capability of two
independent interfaces
� This pattern involves a single class which is responsible to
join functionalities of independent or incompatible
interfaces
40 Systems modelling – Fabrizio Maria Maggi
Adapter
41 Systems modelling – Fabrizio Maria Maggi
Adapter
42 Systems modelling – Fabrizio Maria Maggi
Adapter
43 Systems modelling – Fabrizio Maria Maggi
Adapter
44 Systems modelling – Fabrizio Maria Maggi
Adapter
45 Systems modelling – Fabrizio Maria Maggi
Filter
� Filter pattern or Criteria pattern is a design pattern that
enables developers to filter a set of objects using different
criteria and chaining them in a decoupled way through
logical operations
� This type of design pattern comes under structural
pattern as this pattern combines multiple criteria to
obtain single criteria
46 Systems modelling – Fabrizio Maria Maggi
Filter
47 Systems modelling – Fabrizio Maria Maggi
Filter
48 Systems modelling – Fabrizio Maria Maggi
Filter
49 Systems modelling – Fabrizio Maria Maggi
Filter
50 Systems modelling – Fabrizio Maria Maggi
Filter
51 Systems modelling – Fabrizio Maria Maggi
Filter
52 Systems modelling – Fabrizio Maria Maggi
Filter
53 Systems modelling – Fabrizio Maria Maggi
Filter
54 Systems modelling – Fabrizio Maria Maggi
Composite
� Composite pattern composes objects in term of a tree
structure to represent part as well as whole hierarchy
� This type of design pattern comes under structural
pattern as this pattern creates a tree structure of group
of objects
� This pattern creates a class that contains group of its own
objects: This class provides ways to modify its group of
same objects
55 Systems modelling – Fabrizio Maria Maggi
Composite
56
Composite
57 Systems modelling – Fabrizio Maria Maggi
Decorator
� Decorator pattern allows a user to add new functionality
to an existing object without altering its structure
� This type of design pattern comes under structural
pattern as this pattern acts as a wrapper to existing class
� This pattern creates a decorator class which wraps the
original class and provides additional functionality keeping
class methods signature intact
58 Systems modelling – Fabrizio Maria Maggi
Decorator
59 Systems modelling – Fabrizio Maria Maggi
Decorator
60 Systems modelling – Fabrizio Maria Maggi
Decorator
61 Systems modelling – Fabrizio Maria Maggi
Decorator
62 Systems modelling – Fabrizio Maria Maggi
Decorator
63 Systems modelling – Fabrizio Maria Maggi
Facade
� Facade pattern hides the complexities of the system and
provides an interface to the client using which the client
can access the system
� This type of design pattern comes under structural
pattern as this pattern adds an interface to existing
system to hide its complexities
� This pattern involves a single class which provides
simplified methods required by client and delegates calls to
methods of existing system classes
64 Systems modelling – Fabrizio Maria Maggi
Facade
65 Systems modelling – Fabrizio Maria Maggi
Facade
66 Systems modelling – Fabrizio Maria Maggi
Facade
67 Systems modelling – Fabrizio Maria Maggi
Facade
68
Categories of Design Patterns
69 Systems modelling – Fabrizio Maria Maggi
Command
� Command pattern is a data driven design pattern and falls
under behavioral pattern category
� A request is wrapped under an object as command and
passed to invoker object: Invoker object looks for the
appropriate object which can handle this command and
passes the command to the corresponding object which
executes the command
70 Systems modelling – Fabrizio Maria Maggi
Command
71 Systems modelling – Fabrizio Maria Maggi
Command
72 Systems modelling – Fabrizio Maria Maggi
Command
73 Systems modelling – Fabrizio Maria Maggi
Command
74 Systems modelling – Fabrizio Maria Maggi
Command
75 Systems modelling – Fabrizio Maria Maggi
Iterator
� Iterator pattern is very commonly used design pattern in
Java and .Net programming environment
� This pattern is used to get a way to access the elements of
a collection object in sequential manner without any need
to know its underlying representation
� Iterator pattern falls under behavioral pattern category
76 Systems modelling – Fabrizio Maria Maggi
Iterator
77
Iterator
78
Iterator
79 Systems modelling – Fabrizio Maria Maggi
Observer
� Observer pattern is used when there is one-to-many
relationship between objects such as if one object is
modified, its dependent objects are to be notified
automatically
� Observer pattern falls under behavioral pattern
category
80 Systems modelling – Fabrizio Maria Maggi
Observer
81 Systems modelling – Fabrizio Maria Maggi
Observer
82 Systems modelling – Fabrizio Maria Maggi
Observer
83 Systems modelling – Fabrizio Maria Maggi
Observer
84 Systems modelling – Fabrizio Maria Maggi
Observer
85 Systems modelling – Fabrizio Maria Maggi
Observer
86 Systems modelling – Fabrizio Maria Maggi
Strategy
� In Strategy pattern, a class behavior or its algorithm can be
changed at run time
� This type of design pattern comes under behavioral
pattern
� In Strategy pattern, we create objects which represent
various strategies and a context object whose behavior
varies as per its strategy object: The strategy object
changes the executing algorithm of the context object
87 Systems modelling – Fabrizio Maria Maggi
Strategy
88 Systems modelling – Fabrizio Maria Maggi
Strategy
89 Systems modelling – Fabrizio Maria Maggi
Strategy
90 Systems modelling – Fabrizio Maria Maggi
Strategy
91 Systems modelling – Fabrizio Maria Maggi
Template
� In Template pattern, an abstract class exposes defined
way(s)/template(s) to execute its methods: Its subclasses
can override the method implementation as per need but
the invocation is to be in the same way as defined by an
abstract class
� This pattern comes under behavioral pattern category
92 Systems modelling – Fabrizio Maria Maggi
Template
93 Systems modelling – Fabrizio Maria Maggi
Template
94 Systems modelling – Fabrizio Maria Maggi
Template
95 Systems modelling – Fabrizio Maria Maggi
Template
96 Systems modelling – Fabrizio Maria Maggi
Visitor
� In Visitor pattern, we use a visitor class which changes the
executing algorithm of an element class: By this way,
execution algorithm of element can vary as and when
visitor varies
� This pattern comes under behavioral pattern category
� As per the pattern, element object has to accept the visitor
object so that visitor object handles the operation on the
element object
97 Systems modelling – Fabrizio Maria Maggi
Visitor
98 Systems modelling – Fabrizio Maria Maggi
Visitor
99 Systems modelling – Fabrizio Maria Maggi
Visitor
100 Systems modelling – Fabrizio Maria Maggi
Visitor
101 Systems modelling – Fabrizio Maria Maggi
Visitor
102 Systems modelling – Fabrizio Maria Maggi
Visitor
The Entity-Control-Boundary Pattern
System modelling – Fabrizio Maria Maggi103
http://www.ibm.com
The Entity-Control-Boundary Pattern
System modelling – Fabrizio Maria Maggi104
http://www.ibm.com
The Entity-Control-Boundary Pattern
System modelling – Fabrizio Maria Maggi105
http://www.ibm.com
Functionality to be implemented
The Entity-Control-Boundary Pattern
System modelling – Fabrizio Maria Maggi106
http://www.ibm.com
Functionality to be implementedMost of the calls come from the controller
that plays the role of the „orchestrator“ in
the implementation of the functionality