What is design pattern?
● Design patterns are typical solutions to commonly occurring problems in software design.
● They are like pre-made blueprints that you can customize to solve a recurring design problem in your
code.
● Types of design pattern
○ Creational Design Patterns
○ Structural Design Patterns
○ Behavioral Design Patterns
Creational Design Pattern: Factory Method
we create object without exposing the creation logic to client and the client use the same common interface to
create new type of object
Creational Design Pattern: Builder
● That lets you construct complex objects step by step.
● The pattern allows you to produce different types and representations of an object using the same
construction code.
Creational Design Pattern: Singleton
class Database is
private static field instance: Database
private constructor Database() is
public static method getInstance() is
if (Database.instance == null) then
acquireThreadLock() and then
if (Database.instance == null) then
Database.instance = new Database()
return Database.instance
public method query(sql) is
class Application is
method main() is
Database foo =
Database.getInstance()
foo.query("SELECT ...")
// ...
Database bar =
Database.getInstance()
bar.query("SELECT ...")
// The variable `bar`
will contain the same object as
// the variable `foo`.
That lets you ensure that a class has only one instance, while providing a global access point to this instance.
Creational Design Pattern: Prototype
lets you copy existing objects without making your code dependent on their classes.
Structure Design Pattern: Adapter Pattern
Scenario:
Two incompatible interfaces but the core functionality is same.
Class Adapter - Inheritance Object Adapter - Delegation through Composition
Structure Design Pattern: Bridge Pattern
Scenario:
Vary the implementation and the abstraction by placing the two into separate class hierarchies.
Structure Design Pattern: Composite Pattern
Scenario:
When clients ignore the difference between compositions of objects and individual objects.
Structure Design Pattern: Decorator Pattern
Scenario:
Allows behavior to be added to an individual object, dynamically, without affecting the behavior of other
objects from the same class.
Structure Design Pattern: Flyweight Pattern
Scenario:
Minimize memory usage or computational expenses by sharing as much as possible with similar objects.
Structure Design Pattern: Proxy Pattern
Scenario:
When client needs to protect, hide complexity, or delay expensive actions.
Behaviour Pattern
● Behavioral design patterns are concerned with the interaction and responsibility of objects.
Behaviour Design Pattern: Chain of Responsibility
What it suggests?
Pass the request along the chain
of handlers instead of one.
Control order of request handling
Behaviour Design Pattern: Command
What is suggets?
Turns a request into a stand-alone object
that contains all information about the request.
Behaviour Design Pattern: Iterator
What is suggets?
lets you traverse elements of a collection
without exposing its underlying representation
Behaviour Design Pattern: Meditor
What is suggets?
This pattern restricts direct
communications between the objects and forces
them to collaborate only via a mediator object.
Increases reusability
Behaviour Design Pattern: Template Method
What it says?
Just define the skeleton of a function in
an operation, deferring some steps to its
subclasses.
Remove duplication
Behaviour Design Pattern: Visitor
What is says?
Visitor lets you define a new operation
without changing the classes of the elements on
which it operates.
Behaviour Design Pattern: Memento
Lets you save and restore the previous state of an object without revealing the details of its
implementation.
Behaviour Design Pattern: Observer
Lets you define a subscription mechanism to notify multiple objects about any events that happen to the
object they’re observing.
Behaviour Design Pattern: State
Lets an object alter its behavior when its internal state changes. It appears as if the object changed its
class.
Behaviour Design Pattern: Strategy
Lets you define a family of algorithms, put each of them into a separate class, and make their objects
interchangeable.
Problem Solution