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) Software Development MethodologyDomain Classes; Application Classes (e.g., Patterns); Attributes; Relations; Operations
Domain (Class) Application (Class) Model Interaction Model Modelling
Code
1 Systems modelling – Fabrizio Maria Maggi Application (Class) Model
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 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) .
6 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
7 Categories of Design Patterns
8 Categories of Design Patterns
9 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
10 Systems modelling – Fabrizio Maria Maggi Factory
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 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
15 Systems modelling – Fabrizio Maria Maggi Abstract Factory
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 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
26 Systems modelling – Fabrizio Maria Maggi Singleton
27 Systems modelling – Fabrizio Maria Maggi Singleton
28 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
29 Systems modelling – Fabrizio Maria Maggi Builder
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 Categories of Design Patterns
38 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
39 Systems modelling – Fabrizio Maria Maggi Adapter
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 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
45 Systems modelling – Fabrizio Maria Maggi Filter
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 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
54 Systems modelling – Fabrizio Maria Maggi Composite
55 Systems modelling – Fabrizio Maria Maggi Composite
56 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
57 Systems modelling – Fabrizio Maria Maggi Decorator
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 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
63 Systems modelling – Fabrizio Maria Maggi Facade
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 Categories of Design Patterns
68 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
69 Systems modelling – Fabrizio Maria Maggi 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 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
75 Systems modelling – Fabrizio Maria Maggi Iterator
76 Systems modelling – Fabrizio Maria Maggi Iterator
77 Iterator
78 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
79 Systems modelling – Fabrizio Maria Maggi Observer
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 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
86 Systems modelling – Fabrizio Maria Maggi Strategy
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 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
91 Systems modelling – Fabrizio Maria Maggi Template
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 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
96 Systems modelling – Fabrizio Maria Maggi Visitor
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 http://www.ibm.com
The Entity-Control-Boundary Pattern
103 System modelling – Fabrizio Maria Maggi http://www.ibm.com
The Entity-Control-Boundary Pattern
104 System modelling – Fabrizio Maria Maggi http://www.ibm.com
The Entity-Control-Boundary Pattern
Functionality to be implemented
105 System modelling – Fabrizio Maria Maggi http://www.ibm.com
The Entity-Control-Boundary Pattern
Most of the calls come from the controller Functionality to be implemented that plays the role of the „orchestrator“ in the implementation of the functionality 106 System modelling – Fabrizio Maria Maggi