Design Patterns -Examples
Design Patterns
Façade
Singleton
Adapter
Factory Method
Abstract Factory
Chain of Responsibility
FACADE
Façade
Façade -Example
Façade - Example
Façade- Example
Singleton
Singleton
Singleton – Logger
Problem- Solution-
If your program needs to do
logging, you may want all output With a singleton logger object, you don’t need
to be written to one log file. That to worry about several different objects trying
could lead to resource conflicts to write to the same file, at the same time.
and errors.
Logger - Example
Logger – Client Class(s)
Adapter
Adapter – Object Adapter
Adapter – Class Adapter
Adapter - Example
Let’s say we have a ShopInventory which maintains a list of products. Later on, we
took over another store inventory which sells groceries. We now want to add those
items to our ShopInventory. The problem we have here is that although
the GroceryItem is just a type of product but is unrelated to the Product interface.
Adapter -Example
Adapter- Example
Factory Method
Factory Method
Factory Method- Example
Factory Method
Factory Method - Example
Factory Method -Usage
When you don’t know what class object you need
ahead of time
When all of the potential classes are in the same
subclass hierarchy To centralize class selection code
When you don’t want the user to know every subclass
To encapsulate object creation
Abstract Factory
Abstract Factory
Abstract Factory-
Abstract Factory
Abstract Factory
Abstract Factory
Abstract Factory
Chain of Responsibility
Chain of Responsibility - Usage
Exception Handling mechanism. This qualifies as the
best example for chain of responsibility.
We may have sequence of exceptions listed in
catch statements and when there is an exception
thrown, the catch list is scanned one by one from
top.
If first exception in catch can handle it the job is
done, else the responsibility is moved to next in line
and so on till it reaches finally block if we have one.
Chain of Responsibility-Structure
Chain of Responsibility-UML
Chain of Responsibility-Example
Chain of Responsibility-Example
Chain of Responsibility-Example
Chain of Responsibility -Example