0% found this document useful (0 votes)
24 views

Design Patterns - Examples

This document provides examples of several design patterns including Façade, Singleton, Adapter, Factory Method, Abstract Factory, and Chain of Responsibility. It includes descriptions and examples for each pattern. For Singleton, it describes a common logging problem and how the Singleton pattern solves it by ensuring only one logger object is created. For Adapter, it gives an example of adapting a GroceryItem class to work with an existing Product interface. For Factory Method, it notes some common uses like not knowing object types ahead of time or hiding class details from users.

Uploaded by

Jahanzaib Sajjad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Design Patterns - Examples

This document provides examples of several design patterns including Façade, Singleton, Adapter, Factory Method, Abstract Factory, and Chain of Responsibility. It includes descriptions and examples for each pattern. For Singleton, it describes a common logging problem and how the Singleton pattern solves it by ensuring only one logger object is created. For Adapter, it gives an example of adapting a GroceryItem class to work with an existing Product interface. For Factory Method, it notes some common uses like not knowing object types ahead of time or hiding class details from users.

Uploaded by

Jahanzaib Sajjad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

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

You might also like