an introduction to design patterns · 2019. 9. 2. · singleton singleton pattern is one of the...
TRANSCRIPT
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
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 a 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
16 Systems modelling – Fabrizio Maria Maggi
Singleton
17 Systems modelling – Fabrizio Maria Maggi
Singleton
18
Categories of Design Patterns
19 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
20 Systems modelling – Fabrizio Maria Maggi
Filter
21 Systems modelling – Fabrizio Maria Maggi
Filter
22 Systems modelling – Fabrizio Maria Maggi
Filter
23 Systems modelling – Fabrizio Maria Maggi
Filter
24 Systems modelling – Fabrizio Maria Maggi
Filter
25 Systems modelling – Fabrizio Maria Maggi
Filter
26 Systems modelling – Fabrizio Maria Maggi
Filter
27 Systems modelling – Fabrizio Maria Maggi
Filter
28 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
29 Systems modelling – Fabrizio Maria Maggi
Composite
30
Composite
31 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
32 Systems modelling – Fabrizio Maria Maggi
Facade
33 Systems modelling – Fabrizio Maria Maggi
Facade
34 Systems modelling – Fabrizio Maria Maggi
Facade
35 Systems modelling – Fabrizio Maria Maggi
Facade
36
Categories of Design Patterns
37 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
38 Systems modelling – Fabrizio Maria Maggi
Command
39 Systems modelling – Fabrizio Maria Maggi
Command
40 Systems modelling – Fabrizio Maria Maggi
Command
41 Systems modelling – Fabrizio Maria Maggi
Command
42 Systems modelling – Fabrizio Maria Maggi
Command
43 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
44 Systems modelling – Fabrizio Maria Maggi
Strategy
45 Systems modelling – Fabrizio Maria Maggi
Strategy
46 Systems modelling – Fabrizio Maria Maggi
Strategy
47 Systems modelling – Fabrizio Maria Maggi
Strategy
48 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
49 Systems modelling – Fabrizio Maria Maggi
Template
50 Systems modelling – Fabrizio Maria Maggi
Template
51 Systems modelling – Fabrizio Maria Maggi
Template
52 Systems modelling – Fabrizio Maria Maggi
Template