Design Patterns: by Dr. M. Usman Ashraf

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 57

DESIGN PATTERNS

BY DR. M. USMAN ASHRAF


Motivation

 A
Motivation

 A
The Beginning of Patterns

Christopher Alexander, architect


 A Pattern Language--Towns, Buildings, Construction
 Timeless Way of Building (1979)
 “Each pattern describes a problem which occurs over and over
again in our environment, and then describes the core of the
solution to that problem, in such a way that you can use this
solution a million times over, without ever doing it the same way
twice.”
The “Gang of Four” (GoF) Book

 A
Design Pattern

 A
Design Pattern Elements
Pattern has four elements
 Name

 Problem
 Solution
 Concequences
Design Pattern Elements
Pattern has four elements
 Problem
Design Pattern Elements
Pattern has four elements
 Solution
Design Pattern Elements
Pattern has four elements
 Consequences
Design Patterns Classification

These design patterns provide a way to create objects while hiding the creation logic, rather
than instantiating objects directly using new operator. This gives more flexibility to the program
in deciding which objects need to be created for a given use case.
These design patterns concern class and object composition. Concept of
inheritance is used to compose interfaces and define ways to compose objects
to obtain new functionalities.
These design patterns are specifically concerned with
communication between objects.
Design Patterns Classification

 A
Design Patterns Classification
Design Patterns Classification
Design Patterns Classification
Creational Patterns
1- Factory Method

/Discover
1- Factory Method

A Factory Pattern or Factory Method Pattern says that just define


an interface or abstract class for creating an object but let the
subclasses decide which class to instantiate.
In other words, subclasses are responsible to create the instance of
the class.
1- Factory Method

We're going to create


a Shape interface and concrete
classes implementing
the Shape interface. A factory
class ShapeFactory is defined as a
next step.
FactoryPatternDemo, our demo
class will use ShapeFactory to get
a Shapeobject. It will pass
information (CIRCLE / RECTANGLE
/ SQUARE) to ShapeFactory to get
the type of object it needs.
1- Factory Method
1- Factory Method
2- 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 as this pattern provides one of the best ways to create an
object.
 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.
2- Abstract Factory
3 - 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 as this
pattern provides one of the best ways to create an object.
 A Builder class builds the final object step by step. This builder is
independent of other objects.
3 - Builder

 We have considered a business case of fast-food restaurant where a typical


meal could be a burger and a cold drink. Burger could be either a Veg Burger
or Chicken Burger and will be packed by a wrapper. Cold drink could be either
a coke or pepsi and will be packed in a bottle.
 We are going to create an Item interface representing food items such as
burgers and cold drinks and concrete classes implementing the Item interface
and a Packing interface representing packaging of food items and concrete
classes implementing the Packing interface as burger would be packed in
wrapper and cold drink would be packed as bottle.
 We then create a Meal class having ArrayList of Item and a MealBuilder to
build different types of Meal objects by combining Item.
BuilderPatternDemo, our demo class will use MealBuilder to build a Meal
3 - Builder
4-Prototype

 Prototype pattern refers to creating duplicate object while keeping


performance in mind. This type of design pattern comes under
creational pattern as this pattern provides one of the best ways to
create an object.
 This pattern involves implementing a prototype interface which tells to
create a clone of the current object.
 This pattern is used when creation of object directly is costly. For
example, an object is to be created after a costly database operation.
We can cache the object, returns its clone on next request and update
the database as and when needed thus reducing database calls.
4-Prototype

 We're going to create an abstract class Shape and concrete classes


extending the Shape class. A class ShapeCache is defined as a next
step which stores shape objects in a Hashtable and returns their
clone when requested.
 PrototypPatternDemo, our demo class will use ShapeCache class
to get a Shape object.
4-Prototype
5-Singleton

 Singleton pattern is one of the simplest design patterns in Java.


This type of design pattern comes under creational pattern as this
pattern provides one of the best ways to create an object.
 This pattern ensure that a class has only one instance.
 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.
5-Singleton

 We're going to create a SingleObject class. SingleObject class have


its constructor as private and have a static instance of itself.
 SingleObject class provides a static method to get its static
instance to outside world. SingletonPatternDemo, our demo class
will use SingleObject class to get a SingleObject object.
5-Singleton
Structural Patterns
6-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.
 A real life example could be a case of card reader which acts as an
adapter between memory card and a laptop. You plugin the memory
card into card reader and card reader into the laptop so that memory
card can be read via laptop.
 i.e:, arrays vs. collections
6-Adapter Illustration

 A
7-Bridge

 Bridge is used when we need to decouple an abstraction from its


implementation so that the two can vary independently. This type
of design pattern comes under structural pattern as this pattern
decouples implementation class and abstract class by providing a
bridge structure between them.
 This pattern involves an interface which acts as a bridge which
makes the functionality of concrete classes independent from
interface implementer classes. Both types of classes can be
altered structurally without affecting each other.
7-Bridge Illustration

 A
8-Composite

 Composite pattern is used where we need to treat a group of


objects in similar way as a single object. 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.
Decorator Pattern

 Decorator pattern allows a user to add new functionality to an


existing object without altering its structure.
 Attach features to an object dynamically.
 i.e: Component.
Decorator Illustration

 A
Façade Pattern

 Facade pattern hides the complexities of the system and provides


an interface to the client using which the client can access the
system.
 Provide asimple unified interface to a setof interfacesin a
subsystem
 This pattern involves a single class which provides simplified
methods required by client and delegates calls to methods of
existing system classes.
Façade Illustration

 A
Flyweight Pattern

 Flyweight pattern is primarily used to reduce the number of


objects created and to decrease memory usage and increase
performance.
 Flyweight pattern tries to reuse already existing similar kind
objects by storing them and creates new object when no matching
object is found.
Flyweight Illustration

 A
Proxy Pattern

 In proxy pattern, a class represents functionality of another class.


 In proxy pattern, we create object having original object to
interface its functionality to outer world.
 Delay loading of images till needed.
Proxy Illustration

 A
Behavioral Patterns
Interpreter Pattern

 Interpreter pattern provides a way to evaluate language grammar


or expression.
 This pattern involves implementing an expression interface which
tells to interpret a particular context.
 This pattern is used in SQL parsing, symbol processing engine etc.
 Regular expression matching.
 Define class hierarchy for parse tree.
Interpreter Illustration

 A
Chain of Responsibility Pattern

 As the name suggests, the chain of responsibility pattern creates a


chain of receiver objects for a request.
 This pattern decouples sender and receiver of a request based on
type of request.
 Exception handling could be considered a form of Chain of
Responsibility pattern.
Command Pattern

 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.
 Encapsulate request as object, letting you parameterize clients with
different actions, queue or log requests, etc.
Iterator Pattern

 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.
 i.e: Collections, for-each statement etc.
Mediator Pattern

 Mediator pattern is used to reduce communication complexity


between multiple objects or classes.
 This pattern provides a mediator class which normally handles all
the communications between different classes and supports easy
maintenance of the code by loose coupling.
 The Mediator defines an object that controls how a set of objects
interact. Loose coupling between colleague objects is achieved by
having colleagues communicate with the Mediator, rather than
with each other.
Mediator Illustration
(Air traffic control)
To be continue…

You might also like