Unit3 Ntroduction Design Patterns GoF
Unit3 Ntroduction Design Patterns GoF
PATTER
NS
CONFIDENTIAL
1
HISTORY OF PATTERNS
CONFIDENTI 2
AL
WHAT IS A PATTERN
STOP
Route
A
Route
B
CONFIDENTI 3
AL
WHAT IS DESIGN PATTERN
CONFIDENTI 4
AL
WHAT IS A DESIGN PATTERN
-
Definition
Christopher Alexander
Design patterns are repeatable / reusable
solution to commonly occurring Problems in a
certain context in Software Design
CONFIDENTI 5
AL
Gang of Four (GOF)
CONFIDENTI 6
AL
Gang of Four (GOF)
CONFIDENTI 7
AL
GOF Design Patterns
1. Creational patterns
1. Abstract factory pattern groups object factories that have a common theme.
2. Builder pattern constructs complex objects by separating construction and
representation.
3. Factory method pattern creates objects without specifying the exact class to create.
4. Prototype pattern creates objects by cloning an existing object.
5. Singleton pattern restricts object creation for a class to only one instance.
2. Structural
1. Adapter allows classes with incompatible interfaces to work together by wrapping its
own
interface around that of an already existing class.
2. Bridge decouples an abstraction from its implementation so that the two can vary
independently.
3. Composite composes zero-or-more similar objects so that they can be manipulated as one
object.
4. Decorator dynamically adds/overrides behavior in an existing method of an object.
5. Facade provides a simplified interface to a large body of code.
6. Flyweight reduces the cost of creating and manipulating a large number of similar objects.
7. Proxy provides a placeholder for another object to control access, reduce cost, and reduce
CONFIDENTI
complexity. 8
AL
GOF Design Patterns
3. Behavioral
Patterns
1. Chain of responsibility delegates commands to a chain of processing
2. objects. Command creates objects which encapsulate actions and
3. parameters.
4. Interpreter implements a specialized language.
Iterator accesses the elements of an object sequentially without exposing its
5. underlying representation.
Mediator allows loose coupling between classes by being the only class that
6. has detailed knowledge of their methods.
7. Memento provides the ability to restore an object to its previous state (undo).
Observer is a publish/subscribe pattern which allows a number of observer
objects to
see an event.
8. State allows an object to alter its behavior when its internal state changes.
9. Strategy allows one of a family of algorithms to be selected on-the-fly at
runtime.
10. Template method defines the skeleton of an algorithm as an abstract class,
allowing its
subclasses to provide concrete behavior.
11. Visitor separates an algorithm from an object structure by moving the
hierarchy of
=methods into one object.
CONFIDENTI 9
AL
Design Principle vs Design Pattern
Design Principle
For example, Single Responsibility Principle (SRP) suggests that a class should have only one
and one reason to change. This is high level statement which we can keep in mind while
designing or creating classes for our application. SRP does not provide specific
implementation steps but it's on you how you implement SRP in your application.
Design Pattern
It provides low level solution (implementation) for the commonly occurring object-oriented
problem. In another word, design pattern suggest specific implementation for the specific
object-oriented programming problem. For example, if you want create a class that can only
have one object at a time then you can use Singleton design pattern which suggests the best
way to create a class that can only have one object.
Design patterns are tested by others and safe to follow. E.g. Gang of Four patterns: Abstract
Factory, Factory, Singleton, Command etc.
CONFIDENTI 10
AL
CREATIONAL PATTERNS
BUILDER PATTERN
FACTORY METHOD
PROTOTYPE
ABFPS SINGLETON
CONFIDENTI 12
AL
STRUCTURAL PATTERNS
A B C D F F
P
ADAPTER
BRIDGE
COMPOSITE
DECORATOR
ABCDFFP
FACADE
FLYWEIGHT
PROXY
CONFIDENTI 13
AL
BEHAVIORAL PATTERNS
M M I I C C S S O T
V
MEDIATOR
MEMENTO
INTERPRETER VISITOR
ITERATOR
TEMPLATE
CHAIN OF OBSERVER
RESPONSIBILITY
2 MICS O
n TV COMMAND STRATEGY
(MMIICCSSOT STAT
V) E
CONFIDENTI 14
AL
CREATIONAL PATTERNS
CONFIDENTIAL
15
SINGLETON
Singleton is a creational design pattern that lets you ensure that a class has only
one instance, while providing a global access point to this instance.
The government is an excellent example of the Singleton pattern. A country can have
only one official government.
Travel Card
Government
CONFIDENTI 16
AL
HOW TO ACHIEVE & WHEN TO USE SINGLETON
How to Achieve?
Best Use
• Logging
• Caches
• Registry Settings
• Access External Resources
• Printer
• Device Driver
• Database
CONFIDENTI 17
AL
Builder Pattern
Order Buil
Meal d
Construction
Process of
Buil
Meal
d
Buil
d
CONFIDENTI 19
AL
Builder Pattern
Extend the base House class and create a set of
subclasses to cover all combinations of the
parameters.
CONFIDENTI 20
AL
FACTOR
Y
CONFIDENTI 21
AL
FACTOR
Y
Factory Method is a creational design pattern that provides an interface for creating objects in a
superclass, but allows subclasses to alter the type of objects that will be created.
Adding a new class to the program isn’t that simple if the rest of the code is already
coupled to existing classes.
CONFIDENTI 22
AL
HOW TO ACHIEVE FACTORY
CONFIDENTI 23
AL
HOW TO ACHIEVE FACTORY
CONFIDENTI 24
AL
Abstract Factory
Abstract Factory is a creational design pattern that lets you produce families of related objects
without specifying their concrete classes.
CONFIDENTI 25
AL
Abstract Factory
Animal Factory
Anima Anima
l l
CONFIDENTI 26
AL
Prototype Pattern
Modified
Object
Clone
Cloned Object
Modified
Object
CONFIDENTI 27
AL
STRUCTURAL PATTERNS
CONFIDENTIAL
28
ADAPTER PATTERN
CONFIDENTI 29
AL
ADAPTER PATTERN
CONFIDENTI 30
AL
BRIDGE PATTERN
Bridge is a structural design pattern that lets you split a large class or a set of closely related classes
into two separate hierarchies—abstraction and implementation—which can be developed
independently of each other.
TV and remote are 2 separate entities. As an architect of remote one way is to abstract all
remote in an interface and develop it, however the challenge is in the way both TV and
remote development is getting mixed or interlocked – in technical terms getting tightly
coupled. Changes in one might effect the another. So to solve it, We have created a bridge
between TV and remote. Thus decoupled TV and Remote. Please download example and try
on your own.
CONFIDENTI 31
AL
Bridge Pattern
“Decouple an abstraction from its implementation so that the two can vary independently” is
the intent for bridge design pattern as stated by GoF.
Bridge design pattern is a modified version of the notion of “prefer composition over
inheritance”.Decouple implentation from interface and hiding implementation details from client
is the essence of bridge design pattern.
Bridge is used where we need to decouple an abstraction from its implementation so that the
two can vary independently.
CONFIDENTI 32
AL
COMPOSITE PATTERN
Composite is a structural design pattern that lets you compose objects into tree structures
and then work with these structures as if they were individual objects.
CONFIDENTI 33
AL
COMPOSITE PATTERN
SET OF BRIGADES
CONFIDENTI 34
AL
DECORATOR PATTERN
Decorator is a structural design pattern that lets you attach new behaviors to
objects by placing these objects inside special wrapper objects that contain
the behaviors.
CONFIDENTI 35
AL
FACADE PATTERN
When you call a shop to place a phone order, an operator is your facade to all services
and departments of the shop. The operator provides you with a simple voice interface
to the ordering system, payment gateways, and various delivery services.
CONFIDENTI 36
AL
FLYWEIGHT PATTERN
As much as possible reduction used memory wasted for servicing many similar
objects.
The use of object sharing for effective management of many objects, i.e. we do
not create every object from the beginning, we only base on already created
objects thanks to this we increase the application speed.
CONFIDENTI 37
AL
PROXY PATTERN
INTENT
CONFIDENTI 38
AL
BEHAVIORAL PATTERNS
CONFIDENTIAL
39
CHAIN OF RESPONSIBILITY PATTERN
INTENT
CONFIDENTI 40
AL
COMMAND PATTERN
Command is a behavioral design pattern that turns a request into a stand-alone
object that contains all information about the request. This transformation lets
you parameterize methods with different requests, delay or queue a request’s
execution, and support undoable operations.
CONFIDENTI 41
AL
INTERPRETER PATTERN
• The Interpreter pattern defines a grammatical representation for a
language and an interpreter to interpret the grammar.
• Musicians are examples of Interpreters. The pitch of a sound and its duration
can be represented in musical notation on a staff.
CONFIDENTI 42
AL
ITERATOR PATTERN
Iterator is a behavioral design pattern that lets you traverse elements of a
collection without exposing its underlying representation (list, stack, tree, etc.).
CONFIDENTI 43
AL
ITERATOR PATTERN
The main idea of the Iterator pattern is to extract the traversal behavior of a
collection into a separate object called an iterator.
CONFIDENTI 44
AL
MEDIATOR PATTERN
Aircraft pilots don’t talk to each other directly when deciding who gets to land their plane next.
All communication goes through the control tower.
CONFIDENTI 45
AL
MEMENTO PATTERN
Memento is a behavioral design pattern that lets you save and restore the
previous state of an object without revealing the details of its implementation.
Before executing an operation, the app saves a snapshot of the objects’ state,
which can later be used to restore objects to their previous state.
CONFIDENTI 46
AL
OBSERVER PATTERN
Observer is a behavioral design pattern that lets you define a subscription
mechanism to notify multiple objects about any events that happen to the
object they’re observing.
CONFIDENTI 47
AL
STATE
PATTERN
State is a behavioral design pattern that lets an object alter its behavior when its
internal state changes. It appears as if the object changed its class.
CONFIDENTI 48
AL
STRATEGY PATTERN
CONFIDENTI 49
AL
TEMPLATE PATTERN
A typical architectural plan can be slightly altered to better fit the client’s needs.
CONFIDENTI 50
AL
VISITOR PATTERN
Visitor is a behavioral design pattern that lets you separate algorithms from the
objects on which they operate.
A good insurance agent is always ready to offer different policies to various types of organizations.
CONFIDENTI 51
AL
Q&A
THANK YOU
CONFIDENTI 52
AL