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

What is Design pattern & Advantages

 Known Solution for common problem/requirement


 Not specific to Technology and Language
 Not required to use all patterns in project.
 Easy to adapt and understand

Sensitivity: Internal & Restricted


 What is Design pattern & Advantages
 Types of design pattern
 Creational
 Structural
 Behavioral

Sensitivity: Internal & Restricted


Types of design pattern

Creational
Creational Patterns are used to resolve Object Creation Requirement

Structural
These patterns are used to define a class's structure. The aim of these patterns is to
increase/modify the class functionality, without changing more code.

Behavioral
Behavioral patterns are used to achieve runtime requirement (like define how one class
communicates with others)

MyClass obj = new MyClass();


obj.Method();
Sensitivity: Internal & Restricted
Creational Pattern
 Factory Method
 Abstract Factory
 Builder
 Singleton
 Prototype

Sensitivity: Internal & Restricted


Structural Pattern
 Adapter
 Bridge
 Composite
 Decorator
 Facade
 Flyweight
 Private Class Data
 Proxy.

Sensitivity: Internal & Restricted


Behavioral Pattern
 Chain of responsibility
 Command, Interpreter
 Iterator
 Mediator
 Memento
 Null Object
 Observer
 State
 Strategy
 Template method
 Visitor
Sensitivity: Internal & Restricted
Singleton Pattern
 This design pattern used to create only one object for particular class.
 Singleton class shouldn't have multiple instances in any time.
 Generally this pattern is used for logging, caching and pooling.
public class SingleTon_Class
{
private static SingleTon_Class singleTonObject = new SingleTon_Class();
private SingleTon_Class()
{
//Private Constructor used to avoid instance creation
}
public static SingleTon_Class GetInstance()
{
return singleTonObject;
}
//Public Methods
}

Sensitivity: Internal & Restricted


Factory Pattern
Abstract Factory Pattern

Sensitivity: Internal & Restricted


Factory Pattern & Abstract Factory Pattern
 Both design patterns are part of creational Pattern
 Both Patterns are separates object creation logic
 Both Patterns are used to centralize the objects creation logic in
application.

Sensitivity: Internal & Restricted


Factory Pattern & Abstract Factory Pattern
 Both design patterns are part of creational design pattern
 Both Patterns are separates the object creation logic (Hide the
object creation conditions)
 Both Patterns are used to centralize the objects creation logic in
application.

Interface obj = FactoryClass.GetObject(Type) Objec


tX

… Factory Class
Interface obj = FactoryClass.GetObject(Type) Y
… Object

Sensitivity: Internal & Restricted


Factory Pattern
Creates a Object from several derived classes without
specifying concreate class

Sensitivity: Internal & Restricted


Factory Pattern
Creates an instance of several derived classes
Interface iLogger
Class LogFactory {
{ WriteMessage(String information)
iLogger logger ; }
Switch(LoggerType)
… { EventViewerLogger : iLogger
… case EV: {
iLogger logger = Logfactory.GetObject(); logger = new EventViewerLogger(); WriteMessage(String
Logger.WriteMessage(Message) break; Information{}
… case DB: }
… logger = new DBLogger();
DBLogger : iLogger
break;
{
case FILE:
WriteMessage(String
logger = new FileLogger();
Information{}
break;
}
default:
logger = new EventViewerLogger(); FileLogger : iLogger
} {
} WriteMessage(String
Information{}
Sensitivity: Internal & Restricted }
Factory Pattern
Creates an instance of several derived classes
Interface iLogger
Class LogFactory {
… {
… WriteMessage(String information)
iLogger logger ; }
iLogger logger = Logfactory.GetObject(); Switch(LoggerType)
Logger.WriteMessage(Message) {
… EventViewerLogger : iLogger
case EV: {
… logger = new EventViewerLogger(); WriteMessage(String
break; Information{}
case DB: }
logger = new DBLogger();
DBLogger : iLogger
break;
{
case FILE:
Product - defines the WriteMessage(String
logger = new FileLogger();
interface for factory Information{}
break;
pattern objects }
default:
logger = new EventViewerLogger(); FileLogger : iLogger
} {
} WriteMessage(String
Information{}
Sensitivity: Internal & Restricted }
Factory Pattern
Creates an instance of several derived classes
Interface iLogger
… Class LogFactory {
… { WriteMessage(String information)
iLogger logger = Logfactory.GetObject(); iLogger logger ; }
Logger.WriteMessage(Message) Switch(LoggerType)
… { EventViewerLogger : iLogger
… case EV: {
logger = new EventViewerLogger(); WriteMessage(String
break; Information{}
case DB: }
logger = new DBLogger();
DBLogger : iLogger
break;
{
case FILE:
Concreate Class - WriteMessage(String
logger = new FileLogger();
implements the Information{}
break;
interface }
default:
logger = new EventViewerLogger(); FileLogger : iLogger
} {
} WriteMessage(String
Information{}
Sensitivity: Internal & Restricted }
Factory Pattern
Creates an instance of several derived classes
Interface iLogger
… Class LogFactory {
… { WriteMessage(String information)
iLogger logger = Logfactory.GetObject(); iLogger logger ; }
Logger.WriteMessage(Message) Switch(LoggerType)
… { EventViewerLogger : iLogger
… case EV: {
logger = new EventViewerLogger(); WriteMessage(String
break; Information{}
case DB: }
logger = new DBLogger();
DBLogger : iLogger
Concreate Creator - break;
{
case FILE:
Hide the Object WriteMessage(String
logger = new FileLogger();
Creation logic and Information{}
break;
return appropriate }
default:
concreate class logger = new EventViewerLogger(); FileLogger : iLogger
} {
} WriteMessage(String
Information{}
Sensitivity: Internal & Restricted }
Factory Pattern

Interface
(Product)

Concreate Class A Concreate Class B Concreate Class C

Concreate Creator
Required Object
From Factory

Sensitivity: Internal & Restricted


Abstract Factory Pattern
The Abstract Factory design pattern is used to create
families of related objects without specifying their concrete
classes

Sensitivity: Internal & Restricted


Abstract Factory Pattern

Interface X Interface Y

Concreate Concreate Concreate Concreate


Class X1 Class X2 Class Y1 Class Y2

Abstract Factory
Z
Interface X
Interface Y

Concreate Class Concreate Class


Z1 Z2
Class X1 Class X2
Class Y2 Class Y1

Concreate Creator Required Object From


Return Z1 / Z2 Abstract Factory

Sensitivity: Internal & Restricted


Abstract Factory Pattern
IPhone : iMobile
Interface iMobile Class AssetFactory
Samsung : iMobile {
Assets device ;
AppleLaptop : iSystem Switch(deviceType)
{
Interface iSystem
DellLaptop : iSystem case “FULLTIME”:
device = new ExecutiveAssets();
break;
class ExecutiveAsset : Assets
{ case “CONTRACT”:
iMobile Mobile = new IPhone(); device = new SeniorAssets();
iSystem System = new AppleLaptop(); break;
abstract class Assets }
{ case “TRAINEE”:
iMobile Mobile; device = new JuniorAssets();
class SeniorAsset : Assets
iSystem System; break;
{
} iMobile Mobile=new IPhone(); }
iSystem System = new DellLaptop(); return device;
}
}
class JuniorAsset : Assets
{
iMobile Mobile = new Samsung();
iSystem System = new DellLaptop();
} Sensitivity: Internal & Restricted
Prototype Design Pattern

Sensitivity: Internal & Restricted


Prototype Design Pattern
The Prototype design pattern is used to create Object from existing
objects. When Object Creation is complex and requires more time, then we
can reuse existing object to create new Object

Sensitivity: Internal & Restricted


Prototype Pattern
 Prototype design pattern is part of creational Pattern
 We can Create/Copy/Clone object from objects existing object
 New Object Changes should not affect source object

Advantages
 We will get performance benefit, while creating duplicate
complex object

Sensitivity: Internal & Restricted


Requirement

• We need to get new car price and depreciation values from


external service (which requires additional time)

• Based on these values we need to create object

• We need one more object to calculate old car values for


same model. This new car changes should not impact any
old car parameters

Sensitivity: Internal & Restricted


Builder Design Pattern

Sensitivity: Internal & Restricted


Builder Design Pattern
The Builder design pattern is used to separate the complex object
creation process. Same Object construction process is used to create
different objects.

Sensitivity: Internal & Restricted


Builder Pattern
 Builder design pattern is part of creational Pattern
 Separates the complex object creation logic. The same
construction process used to create different object
 This pattern used to create fully initialized objects
Disadvantages
 Suitable only for complex object creation
 Requires additional code

Sensitivity: Internal & Restricted


eCommerce Portal

Desktop /
Processor Memory
Laptop

Additional Storage
Checkout
Devices

Sensitivity: Internal & Restricted


Laptop A= new Laptop();
//Create laptop with Multiple constructor
(Or)
A.setConfig(Collection item)
(Or)
A.SetProcessor(p).SetStorage(s).SetMemory(m);
eCommerce Portal
----------------

Desktop B= new Desktop();


//Create Desktop with Multiple constructor
(Or)
B.setConfig(Collection item)
(Or)
B.SetProcessor(p).SetStorage(s).SetMemory(m);

Sensitivity: Internal & Restricted


Laptop
{
SetProcessor(string P)
{ Processor=p; }
SetMonitor(string M) Laptop A= new Laptop();
{ Monitor=M; } A.SetProcessor(p)
SetStorage(string S) .SetMemory(m)
{ Storage=S; }
.SetStorage(s)
SetAdditional(string M)
{ Monitor=M; } .SetAdditional(m)
}
---------------
eCommerce Portal

Desktop B= new Desktop();


Desktop B.SetProcessor(p)
{ .SetMemory(m)
SetProcessor(string P)
.SetStorage(s)
{ Processor=p; }
SetMonitor(string M) . SetAdditional(m)
{ Monitor=M; }
SetStorage(string S)
{ Storage=S; }
SetAdditional(string M)
{ Monitor=M; }
}

Sensitivity: Internal & Restricted


Director ObjectBuilder = new Director();

IBuilder builder = new LaptopBuilder();

IProduct Obj = ObjectBuilder.CreateProduct(builder,"I7,32GB,1TB,Mouse &


Keyboard");

Sensitivity: Internal & Restricted


Director ObjectBuilder = new Director();

IBuilder builder = new LaptopBuilder();

IProduct Obj = ObjectBuilder.CreateProduct(builder,"I7,32GB,1TB,Inbuild Mouse &


Keyboard");

Obj.ShowDetails();

builder = new DesktopBuilder();

Obj = ObjectBuilder.CreateProduct(builder, "I5,64GB,2TB,Bluetooth Mouse & Keyboard");

Obj.ShowDetails();

Sensitivity: Internal & Restricted


LaptopBuilder :
IBuilder
{
//Create Laptop Object
// Build laptop Object
// Return Laptop Object
}

Director
{
IBuilder CreateProduct (IBuilder B, string
Parm)
CreateProduct() {
Initialize(); B.CreateProduct()
ReturnProduct(); B.Initialize();
B.ReturnProduct();
}
}

DesktopBuilder :
IBuilder
{
//Create Laptop Object
// Build laptop Object
// Return Laptop Object
}

Sensitivity: Internal & Restricted


Laptop : IProduct LaptopBuilder :
{ IBuilder
SetProcessor(string P) { } {
SetMemory(string M) { } CreateProduct() {}
SetStorage(string S) { } Initialize() {}
ShowDetails() { } ReturnProduct() {}
} }

Director
{
IProduct IBuilder CreateProduct (IBuilder B, string
Parm)
SetProcessor CreateProduct() {
SetMemory Initialize(); B.CreateProduct()
SetStorage ReturnProduct(); B.Initialize();
SetMonitor
B.ReturnProduct();
}
}

Desktop : IProduct DesktopBuilder :


{ IBuilder
SetProcessor(string P) { } {
SetMemory(string M) { } CreateProduct() {}
SetStorage(string S) { } Initialize() {}
ShowDetails() { } ReturnProduct() {}
} }

Sensitivity: Internal & Restricted


Laptop : Iproduct
LaptopBuilder : Ibuilder
Concreate
Builder Concreate
Product
Product

Director
Iproduct IBuilder
Product Director

Builder Product

Desktop : Iproduct
DesktopBuilder :
Concreate Ibuilder
Product
Builder Concreate
Product

Sensitivity: Internal & Restricted


Structural Design Pattern

Sensitivity: Internal & Restricted


Structural Pattern
These patterns are used to define a class's structure.

Advantage

 These patterns is to increase/modify the class functionality, without


changing more code.

 We can combine and build a large structure with this pattern

Sensitivity: Internal & Restricted


Adapter & Decorator Pattern

Sensitivity: Internal & Restricted


Adapter Pattern
Adapter pattern acts as a bridge between two incompatible
interfaces. We can use this design pattern, when we are using old
incompatible interfaces .

Sensitivity: Internal & Restricted


Adapter Pattern
Adapter pattern acts as a bridge between two incompatible
interfaces. We can use this design pattern, when we are using incompatible
interfaces .

Customer
Application A GetReorts()

iEntity Obj= new Customer();


Obj.GetReport(); iEntity
iEntity Obj= new GetReorts()
Employee();
Obj.GetReport(); Employee
GetReorts()

Sensitivity: Internal & Restricted


Adapter Pattern
Adapter pattern acts as a bridge between two incompatible
interfaces. We can use this design pattern, when we are using incompatible
interfaces .

Customer
Application B GetReorts()
iEntity Obj= new Customer();
Obj.GetPdfReport();
iEntity
iEntity Obj= new
GetReorts()
Employee();
Obj.GetPdfReport();
Employee
GetReorts()

Sensitivity: Internal & Restricted


Adapter Pattern
Adapter pattern acts as a bridge between two incompatible
interfaces. We can use this design pattern, when we are using incompatible
interfaces .

Customer
GetReorts()
Application B
iReport
iReport Obj= new CustReport(); iEntity
Obj.GetPdfReport(); GetPdfReport
GetHTMLReport GetReorts()
iReport Obj= new EmpReport();
Obj. GetPdfReport();
GetXMLReport
Employee
GetReorts()

Sensitivity: Internal & Restricted


Adapter Pattern
Adapter pattern acts as a bridge between two incompatible
interfaces. We can use this design pattern, when we are using incompatible
interfaces .
Application A
iEntity Obj= new
Customer(); Customer
Obj.GetReport();

iEntity Obj= new


Employee();
Obj.GetReport();
iEntity
CustReport
Application B iReport
iReport Obj= new CustReport(); GetPdfReport
Obj.GetPdfReport();
GetHTMLReport
iReport Obj= new EmpReport(); GetXMLReport
Obj. GetPdfReport(); EmpReport Employee

Sensitivity: Internal & Restricted


Adapter Pattern
Adapter pattern acts as a bridge between two incompatible
interfaces. We can use this design pattern, when we are using incompatible
interfaces .

CustReport Customer
Application

Sensitivity: Internal & Restricted


Decorator Pattern
Decorator pattern includes additional functionality to an existing
object. We are decorating the existing functionality without modifying
the original object. So, we can call this pattern as Wrapper pattern

Sensitivity: Internal & Restricted


Decorator Pattern
Adapter pattern includes additional functionality to an existing object.
We are decorating the existing functionality without modifying the original
object. So, we can call this pattern as Wrapper pattern

Customer
iEntity Obj= new Customer();
Obj.GetReport();

iEntity Obj= new iEntity


Employee();
Obj.GetReport();

Employee

Sensitivity: Internal & Restricted


Decorator Pattern
Decorator pattern includes additional functionality to an existing object.
We are decorating the existing functionality without modifying the original
object. So, we can call this pattern as Wrapper pattern

Cust_Report
Customer
ReportHeader()
Customer.GetReport GetReports()
()
iEntity Obj= new ReportFooter()
Cust_Report();
Obj.GetReport(); iEntity
iEntity Obj= new GetReports()
Emp_Report();
Obj.GetReport();

Emp_Report
Employee
ReportHeader()
GetReports()
Employee.GetReport(
)
ReportFooter()
Sensitivity: Internal & Restricted
Adapter Pattern
Convert one class in to another class

Decorator Pattern
Include additional functionality for same class

Sensitivity: Internal & Restricted


Façade and Proxy Pattern

Sensitivity: Internal & Restricted


Façade Pattern

Unified single place for collection of subsystem. All the


subsystem (classes) are managed within Façade class. So, Client code
doesn’t have any dependency with subsystem

This pattern is useful, when we are using complex large framework


which contain more classes

Sensitivity: Internal & Restricted


Façade Pattern
Unified interface for collection of subsystem. All the subsystem
(classes) are managed in Façade class. So, Client code doesn’t have any
dependency with subsystem

SubSystem 1

Client Application
Façade Class
SubSystem 2
FaçadeClass.Do() SubSystem1.do()
SubSystem2.do()
SubSystem3.do()

SubSystem 3

Sensitivity: Internal & Restricted


Façade Pattern
Unified interface for collection of subsystem. All the subsystem
(classes) are managed in Façade class. So, Client code doesn’t have any
dependency with subsystem

Gateway
ProcessPayment()

Client Application
Façade Class Inventory
Façade.PurchaseTicket( Gateway.ProcessPayment(
) ) UpdateInventory()
Inventory.UpdateInventory
()
Notification.SendTicket()

Notification
SendTicket()

Sensitivity: Internal & Restricted


Proxy Pattern

Proxy is a class which is representing the another class. It will


forward request to original implementation. But, wont modify / add original
implementation (like decorator)

Sensitivity: Internal & Restricted


Proxy Pattern

Proxy is a class which is representing the another class. It will


forward request to original implementation. But, wont modify / add
original implementation (like decorator)

Advantages

 Validate the Request / Parameter


 Check access permission
 Encode Parameters

Sensitivity: Internal & Restricted


Proxy Pattern
Proxy is a class which is representing the another class. It will
forward request to original implementation.

Client Application
Proxy Class MainClass
ProxyClass.Do() Authentication.do Network /
() Local Do()
ValidRequest.do()
Proxy.do()

Sensitivity: Internal & Restricted


Bridge Design pattern

Sensitivity: Internal & Restricted


Bridge Design Pattern [Structural Pattern]
Split the abstraction from its implementation is called Bridge design
pattern. Split the class into multiple small class and each class can be
developed independently.
Advantages
 New abstractions and implementations can be developed independently
 Improved Extensibility
 Used to implement Open-Close Design Principle

Sensitivity: Internal & Restricted

You might also like