an introduction to design patterns · 2019. 9. 2. · singleton singleton pattern is one of the...

53
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)

Upload: others

Post on 15-Mar-2021

5 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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)

Page 2: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 3: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

2 Systems modelling – Fabrizio Maria Maggi

Application (Class) Model

Page 4: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

3 Systems modelling – Fabrizio Maria Maggi

Application (Class) Model

Page 5: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

4 Systems modelling – Fabrizio Maria Maggi

Application (Class) Model

Page 6: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

5 Systems modelling – Fabrizio Maria Maggi

Application (Class) Model

Page 7: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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).

Page 8: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 9: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

8

Categories of Design Patterns

Page 10: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

9

Categories of Design Patterns

Page 11: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 12: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

11 Systems modelling – Fabrizio Maria Maggi

Factory

Page 13: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

12 Systems modelling – Fabrizio Maria Maggi

Factory

Page 14: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

13 Systems modelling – Fabrizio Maria Maggi

Factory

Page 15: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

14 Systems modelling – Fabrizio Maria Maggi

Factory

Page 16: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 17: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

16 Systems modelling – Fabrizio Maria Maggi

Singleton

Page 18: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

17 Systems modelling – Fabrizio Maria Maggi

Singleton

Page 19: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

18

Categories of Design Patterns

Page 20: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 21: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

20 Systems modelling – Fabrizio Maria Maggi

Filter

Page 22: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

21 Systems modelling – Fabrizio Maria Maggi

Filter

Page 23: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

22 Systems modelling – Fabrizio Maria Maggi

Filter

Page 24: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

23 Systems modelling – Fabrizio Maria Maggi

Filter

Page 25: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

24 Systems modelling – Fabrizio Maria Maggi

Filter

Page 26: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

25 Systems modelling – Fabrizio Maria Maggi

Filter

Page 27: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

26 Systems modelling – Fabrizio Maria Maggi

Filter

Page 28: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

27 Systems modelling – Fabrizio Maria Maggi

Filter

Page 29: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 30: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

29 Systems modelling – Fabrizio Maria Maggi

Composite

Page 31: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

30

Composite

Page 32: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 33: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

32 Systems modelling – Fabrizio Maria Maggi

Facade

Page 34: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

33 Systems modelling – Fabrizio Maria Maggi

Facade

Page 35: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

34 Systems modelling – Fabrizio Maria Maggi

Facade

Page 36: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

35 Systems modelling – Fabrizio Maria Maggi

Facade

Page 37: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

36

Categories of Design Patterns

Page 38: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 39: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

38 Systems modelling – Fabrizio Maria Maggi

Command

Page 40: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

39 Systems modelling – Fabrizio Maria Maggi

Command

Page 41: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

40 Systems modelling – Fabrizio Maria Maggi

Command

Page 42: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

41 Systems modelling – Fabrizio Maria Maggi

Command

Page 43: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

42 Systems modelling – Fabrizio Maria Maggi

Command

Page 44: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 45: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

44 Systems modelling – Fabrizio Maria Maggi

Strategy

Page 46: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

45 Systems modelling – Fabrizio Maria Maggi

Strategy

Page 47: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

46 Systems modelling – Fabrizio Maria Maggi

Strategy

Page 48: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

47 Systems modelling – Fabrizio Maria Maggi

Strategy

Page 49: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

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

Page 50: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

49 Systems modelling – Fabrizio Maria Maggi

Template

Page 51: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

50 Systems modelling – Fabrizio Maria Maggi

Template

Page 52: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

51 Systems modelling – Fabrizio Maria Maggi

Template

Page 53: An Introduction to Design Patterns · 2019. 9. 2. · Singleton Singleton pattern is one of the simplest design patterns in Java This type of design pattern comes under creational

52 Systems modelling – Fabrizio Maria Maggi

Template