What is design pattern

Khulna University of Engineering & Technology (KUET)
Khulna University of Engineering & Technology (KUET)Khulna University of Engineering & Technology (KUET)
Topics : Design patterns in java
created by……
Md .shohel Rana
Roll:140135
2nd year 2nd semester
Dept. of Cse
PUST
introduction
 What is design patterns ?
 Why need design patterns ?
 What types of design patterns ?
 Describing design patterns.
Design pattern is a general reusable solution to a commonly
occurring Problem in software design.
A design pattern is not a finished design that can be
transformed directly into code . It is a description or template
for how to solve a problem that can be used in many different
situations.
A designer who is familiar with such patterns can apply them
immediately to design problems without having to rediscover
them.
What is Design pattern
Why need design pattern
To design a new software system quickly and efficiently.
To understand a existing software system.
Types of Design pattern
Creational design pattern.
--concerned with everything about the creation of objects.
Structural design patterns.
--concerned with how classes and objects are composed to
form larger structures.
Behavioural design patterns.
--communication between objects.
1) Singleton design pattern(Creational pattern)
Ensure that only one instance of a class is created.
Provide a global point of access to the object.
In other words, a class must ensure that only single
instance should be created and single object can be used
by all other classes.
How to create Singleton design pattern?
To create the singleton class, we need to have static member of class,
private constructor and static factory method.
 Static member: It gets memory only once because of static,
itcontains the instance of the Singleton class.
 Private constructor: It will prevent to instantiate the Singleton class
from outside the class.
 Static factory method: This provides the global point of access to the
Singleton object and returns the instance to the caller.
 We need.
1) Private static instance of class
2) Private constractor
3) Public static method with return type of class to access that instance.
UML -class diagram for singleton
There are two forms of singleton design pattern
1) Early mode: creation of instance at load time.
2) Lazy mode: creation of instance when required.
Example:1
BangladeshPresident.java
Understanding early mode of Singleton Pattern
class BangladeshPresident{
private static PresidentObj=new President();//Early, instance will be created at load
time
private BangladeshPresident(){}
public static A getPresident (){
return PresidentObj;
}
public void doSomething(){
//write your code
}
}
Understanding lazy modeof Singleton Pattern
class BangladeshPresident{
private static A PresidentObj;
private BangladeshPresident(){}
public static A getPresident (){
if (PresidentObj == null){
PresidentObj = new BangladeshPresident();//instance will be
created at request time
}
return PresidentObj;
}
Example 2:
Advantage of Singleton design pattern
Saves memory because object is not created at each
request.only single instance is reused again and again.
In cases when object creation is very costly (time taking),
we don’t have to create new object each time we need it.
We just access already created object.
Applications
 Singleton pattern is mostly used in multi-threaded and database
applications.
 It is used in logging ,configuration settings etc.
 For database connection , because one connection is enough for
most applications and too much connections can make application
slow.
2) Decorator design pattern (Structural Patterns)
 Decorator design pattern is used to modify the functionality or
characteristics of an object at runtime or dynamically. At the same
time other instances of the same class will not be affected by this,
so individual object gets the new modified behavior.Unlike
inheritance, you can choose any single object of a class and modify
its behaviour leaving the other instances unmodified.
 In implementing the decorator pattern you construct a wrapper
around an object by extending its behavior. The wrapper will do its
job before or after and delegate the call to the wrapped instance.
When to Use decorate design pattern
 We use inheritance or composition to extend the behavior of an object but this is
done at compile time and its applicable to all the instances of the class. We
can’t add any new functionality of remove any existing behavior at runtime – this
is when Decorator pattern comes into picture.
 to add responsibilities to individual objects dynamically without affecting other
objects.
 When extension by subclassing is impractical. Sometimes, a large number of
independent extensions are possible and would produce an explosion of
subclasses to support every combination. Or class definition may be hidden or
otherwise unavailable for subclassing.
UML class diagram for decorator design
Elements
 Component
defines interface for objects that can have additional responsibilities
added to them.
 ConcreteComponent
defines an object on which additional responsibilities can be added
 Decorator
maintains a reference to component object and defines an interface that
conforms to component's interface. In simple words, it has a component
object to be decorated.
 ConcreteDecorator
add responsibilities to the component object.
Example
gvhghBuilding
Advantages of Decorator Design Pattern
It is flexible than inheritance because inheritance adds
responsibility at compile time but decorator pattern adds
at run time.
We can have any number of decorators and also in any
order.
It extends functionality of object without affecting any
other object.
Disadvantage of Decorator Design Pattern
The main disadvantage of decorator design pattern is
code maintainability because this pattern creates lots of
similar decorators which are sometimes hard to maintain
and distinguish.
Any questions……?
thanks
1 de 25

Recomendados

Design Pattern - Factory Method Pattern por
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternMudasir Qazi
5.1K visualizações13 slides
Introduction to Design Pattern por
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design PatternSanae BEKKAR
17K visualizações31 slides
Creational pattern por
Creational patternCreational pattern
Creational patternHimanshu
3.3K visualizações20 slides
Java Course 11: Design Patterns por
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design PatternsAnton Keks
6.6K visualizações36 slides
Design pattern por
Design patternDesign pattern
Design patternThibaut De Broca
3.8K visualizações61 slides
Design Patterns por
Design PatternsDesign Patterns
Design PatternsAnuja Arosha
13.5K visualizações56 slides

Mais conteúdo relacionado

Mais procurados

Software design patterns ppt por
Software design patterns pptSoftware design patterns ppt
Software design patterns pptmkruthika
29.9K visualizações48 slides
Design pattern-presentation por
Design pattern-presentationDesign pattern-presentation
Design pattern-presentationRana Muhammad Asif
3.3K visualizações47 slides
Introduction to design patterns por
Introduction to design patternsIntroduction to design patterns
Introduction to design patternsAmit Kabra
1.7K visualizações44 slides
Let us understand design pattern por
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
35.8K visualizações45 slides
Java Design Patterns Tutorial | Edureka por
Java Design Patterns Tutorial | EdurekaJava Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | EdurekaEdureka!
1.3K visualizações45 slides
Design Patterns - 01 Introduction and Decorator Pattern por
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Patterneprafulla
4.6K visualizações17 slides

Mais procurados(20)

Software design patterns ppt por mkruthika
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
mkruthika29.9K visualizações
Design pattern-presentation por Rana Muhammad Asif
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
Rana Muhammad Asif3.3K visualizações
Introduction to design patterns por Amit Kabra
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
Amit Kabra1.7K visualizações
Let us understand design pattern por Mindfire Solutions
Let us understand design patternLet us understand design pattern
Let us understand design pattern
Mindfire Solutions35.8K visualizações
Java Design Patterns Tutorial | Edureka por Edureka!
Java Design Patterns Tutorial | EdurekaJava Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | Edureka
Edureka!1.3K visualizações
Design Patterns - 01 Introduction and Decorator Pattern por eprafulla
Design Patterns - 01 Introduction and Decorator PatternDesign Patterns - 01 Introduction and Decorator Pattern
Design Patterns - 01 Introduction and Decorator Pattern
eprafulla4.6K visualizações
Software Design Patterns por Satheesh Sukumaran
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Satheesh Sukumaran1.2K visualizações
Gof design patterns por Srikanth R Vaka
Gof design patternsGof design patterns
Gof design patterns
Srikanth R Vaka19.1K visualizações
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript por pcnmtutorials
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
pcnmtutorials710 visualizações
Design Pattern For C# Part 1 por Shahzad
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
Shahzad 7.1K visualizações
Design Patterns - General Introduction por Asma CHERIF
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
Asma CHERIF627 visualizações
Facade Pattern por melbournepatterns
Facade PatternFacade Pattern
Facade Pattern
melbournepatterns5.8K visualizações
Facade pattern por Shakil Ahmed
Facade patternFacade pattern
Facade pattern
Shakil Ahmed1.7K visualizações
Design Patterns Presentation - Chetan Gole por Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
Chetan Gole12.1K visualizações
Spring Framework - Core por Dzmitry Naskou
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou34.8K visualizações
Software Engineering - chp4- design patterns por Lilia Sfaxi
Software Engineering - chp4- design patternsSoftware Engineering - chp4- design patterns
Software Engineering - chp4- design patterns
Lilia Sfaxi14K visualizações
Builder pattern por Shakil Ahmed
Builder patternBuilder pattern
Builder pattern
Shakil Ahmed6.7K visualizações
Design patterns por abhisheksagi
Design patternsDesign patterns
Design patterns
abhisheksagi14.9K visualizações
Adapter pattern por Shakil Ahmed
Adapter patternAdapter pattern
Adapter pattern
Shakil Ahmed5.7K visualizações
Design Patterns por soms_1
Design PatternsDesign Patterns
Design Patterns
soms_114.2K visualizações

Similar a What is design pattern

P Training Presentation por
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
1.1K visualizações192 slides
Design patterns in javascript por
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
125 visualizações60 slides
Introduction to Design Patterns por
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design PatternsPrageeth Sandakalum
1.4K visualizações25 slides
7 software design patterns you should know in 2022 por
7 software design patterns you should know in 20227 software design patterns you should know in 2022
7 software design patterns you should know in 2022Growth Natives
111 visualizações1 slide
Design patterns por
Design patternsDesign patterns
Design patternsF(x) Data Labs Pvt Ltd
207 visualizações27 slides
Why Design Patterns Are Important In Software Engineering por
Why Design Patterns Are Important In Software EngineeringWhy Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software EngineeringProtelo, Inc.
4.6K visualizações8 slides

Similar a What is design pattern(20)

P Training Presentation por Gaurav Tyagi
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi1.1K visualizações
Design patterns in javascript por Ayush Sharma
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
Ayush Sharma125 visualizações
Introduction to Design Patterns por Prageeth Sandakalum
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
Prageeth Sandakalum1.4K visualizações
7 software design patterns you should know in 2022 por Growth Natives
7 software design patterns you should know in 20227 software design patterns you should know in 2022
7 software design patterns you should know in 2022
Growth Natives111 visualizações
Why Design Patterns Are Important In Software Engineering por Protelo, Inc.
Why Design Patterns Are Important In Software EngineeringWhy Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software Engineering
Protelo, Inc.4.6K visualizações
Sofwear deasign and need of design pattern por chetankane
Sofwear deasign and need of design patternSofwear deasign and need of design pattern
Sofwear deasign and need of design pattern
chetankane1.8K visualizações
Patterns for the People por Kevlin Henney
Patterns for the PeoplePatterns for the People
Patterns for the People
Kevlin Henney1.1K visualizações
Gang of Four in Java por Mina Tafreshi
Gang of Four in Java Gang of Four in Java
Gang of Four in Java
Mina Tafreshi97 visualizações
Jump start to OOP, OOAD, and Design Pattern por Nishith Shukla
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
Nishith Shukla9.8K visualizações
Jump Start To Ooad And Design Patterns por Lalit Kale
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
Lalit Kale1.2K visualizações
Oops design pattern intro por anshu_atri
Oops design pattern intro Oops design pattern intro
Oops design pattern intro
anshu_atri27 visualizações
Design Patterns For 70% Of Programmers In The World por Saurabh Moody
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The World
Saurabh Moody24.8K visualizações
Sda 8 por AmberMughal5
Sda   8Sda   8
Sda 8
AmberMughal566 visualizações
Introduction To Design Patterns por sukumarraju6
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
sukumarraju6372 visualizações
Design Pattern Notes: Nagpur University por Shubham Narkhede
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur University
Shubham Narkhede1.4K visualizações
Design Patterns por Sergii Stets
Design PatternsDesign Patterns
Design Patterns
Sergii Stets344 visualizações
Design pattern in android por Jay Kumarr
Design pattern in androidDesign pattern in android
Design pattern in android
Jay Kumarr21K visualizações
Design Patterns por Rafael Coutinho
Design PatternsDesign Patterns
Design Patterns
Rafael Coutinho1.4K visualizações
Design patterns por mudabbirwarsi
Design patternsDesign patterns
Design patterns
mudabbirwarsi1.8K visualizações

Último

Generic or specific? Making sensible software design decisions por
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
6 visualizações60 slides
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... por
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...TomHalpin9
6 visualizações29 slides
Software evolution understanding: Automatic extraction of software identifier... por
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...Ra'Fat Al-Msie'deen
9 visualizações33 slides
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P... por
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...NimaTorabi2
12 visualizações17 slides
Short_Story_PPT.pdf por
Short_Story_PPT.pdfShort_Story_PPT.pdf
Short_Story_PPT.pdfutkarshsatishkumarsh
5 visualizações16 slides
FIMA 2023 Neo4j & FS - Entity Resolution.pptx por
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptxNeo4j
8 visualizações26 slides

Último(20)

Generic or specific? Making sensible software design decisions por Bert Jan Schrijver
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
Bert Jan Schrijver6 visualizações
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... por TomHalpin9
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
TomHalpin96 visualizações
Software evolution understanding: Automatic extraction of software identifier... por Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
Ra'Fat Al-Msie'deen9 visualizações
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P... por NimaTorabi2
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
NimaTorabi212 visualizações
FIMA 2023 Neo4j & FS - Entity Resolution.pptx por Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j8 visualizações
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... por Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller40 visualizações
Copilot Prompting Toolkit_All Resources.pdf por Riccardo Zamana
Copilot Prompting Toolkit_All Resources.pdfCopilot Prompting Toolkit_All Resources.pdf
Copilot Prompting Toolkit_All Resources.pdf
Riccardo Zamana10 visualizações
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx por animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm15 visualizações
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols por Deltares
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - DolsDSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
Deltares9 visualizações
nintendo_64.pptx por paiga02016
nintendo_64.pptxnintendo_64.pptx
nintendo_64.pptx
paiga020165 visualizações
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... por Donato Onofri
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Donato Onofri860 visualizações
Navigating container technology for enhanced security by Niklas Saari por Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy14 visualizações
Agile 101 por John Valentino
Agile 101Agile 101
Agile 101
John Valentino9 visualizações
MS PowerPoint.pptx por Litty Sylus
MS PowerPoint.pptxMS PowerPoint.pptx
MS PowerPoint.pptx
Litty Sylus5 visualizações
DSD-INT 2023 Process-based modelling of salt marsh development coupling Delft... por Deltares
DSD-INT 2023 Process-based modelling of salt marsh development coupling Delft...DSD-INT 2023 Process-based modelling of salt marsh development coupling Delft...
DSD-INT 2023 Process-based modelling of salt marsh development coupling Delft...
Deltares7 visualizações
FOSSLight Community Day 2023-11-30 por Shane Coughlan
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30
Shane Coughlan5 visualizações
HarshithAkkapelli_Presentation.pdf por harshithakkapelli
HarshithAkkapelli_Presentation.pdfHarshithAkkapelli_Presentation.pdf
HarshithAkkapelli_Presentation.pdf
harshithakkapelli11 visualizações
tecnologia18.docx por nosi6702
tecnologia18.docxtecnologia18.docx
tecnologia18.docx
nosi67025 visualizações
EV Charging App Case por iCoderz Solutions
EV Charging App Case EV Charging App Case
EV Charging App Case
iCoderz Solutions5 visualizações

What is design pattern

  • 1. Topics : Design patterns in java created by…… Md .shohel Rana Roll:140135 2nd year 2nd semester Dept. of Cse PUST
  • 2. introduction  What is design patterns ?  Why need design patterns ?  What types of design patterns ?  Describing design patterns.
  • 3. Design pattern is a general reusable solution to a commonly occurring Problem in software design. A design pattern is not a finished design that can be transformed directly into code . It is a description or template for how to solve a problem that can be used in many different situations. A designer who is familiar with such patterns can apply them immediately to design problems without having to rediscover them. What is Design pattern
  • 4. Why need design pattern To design a new software system quickly and efficiently. To understand a existing software system.
  • 5. Types of Design pattern Creational design pattern. --concerned with everything about the creation of objects. Structural design patterns. --concerned with how classes and objects are composed to form larger structures. Behavioural design patterns. --communication between objects.
  • 6. 1) Singleton design pattern(Creational pattern) Ensure that only one instance of a class is created. Provide a global point of access to the object. In other words, a class must ensure that only single instance should be created and single object can be used by all other classes.
  • 7. How to create Singleton design pattern? To create the singleton class, we need to have static member of class, private constructor and static factory method.  Static member: It gets memory only once because of static, itcontains the instance of the Singleton class.  Private constructor: It will prevent to instantiate the Singleton class from outside the class.  Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller.
  • 8.  We need. 1) Private static instance of class 2) Private constractor 3) Public static method with return type of class to access that instance. UML -class diagram for singleton
  • 9. There are two forms of singleton design pattern 1) Early mode: creation of instance at load time. 2) Lazy mode: creation of instance when required.
  • 11. Understanding early mode of Singleton Pattern class BangladeshPresident{ private static PresidentObj=new President();//Early, instance will be created at load time private BangladeshPresident(){} public static A getPresident (){ return PresidentObj; } public void doSomething(){ //write your code } }
  • 12. Understanding lazy modeof Singleton Pattern class BangladeshPresident{ private static A PresidentObj; private BangladeshPresident(){} public static A getPresident (){ if (PresidentObj == null){ PresidentObj = new BangladeshPresident();//instance will be created at request time } return PresidentObj; }
  • 14. Advantage of Singleton design pattern Saves memory because object is not created at each request.only single instance is reused again and again. In cases when object creation is very costly (time taking), we don’t have to create new object each time we need it. We just access already created object.
  • 15. Applications  Singleton pattern is mostly used in multi-threaded and database applications.  It is used in logging ,configuration settings etc.  For database connection , because one connection is enough for most applications and too much connections can make application slow.
  • 16. 2) Decorator design pattern (Structural Patterns)  Decorator design pattern is used to modify the functionality or characteristics of an object at runtime or dynamically. At the same time other instances of the same class will not be affected by this, so individual object gets the new modified behavior.Unlike inheritance, you can choose any single object of a class and modify its behaviour leaving the other instances unmodified.  In implementing the decorator pattern you construct a wrapper around an object by extending its behavior. The wrapper will do its job before or after and delegate the call to the wrapped instance.
  • 17. When to Use decorate design pattern  We use inheritance or composition to extend the behavior of an object but this is done at compile time and its applicable to all the instances of the class. We can’t add any new functionality of remove any existing behavior at runtime – this is when Decorator pattern comes into picture.  to add responsibilities to individual objects dynamically without affecting other objects.  When extension by subclassing is impractical. Sometimes, a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or class definition may be hidden or otherwise unavailable for subclassing.
  • 18. UML class diagram for decorator design
  • 19. Elements  Component defines interface for objects that can have additional responsibilities added to them.  ConcreteComponent defines an object on which additional responsibilities can be added  Decorator maintains a reference to component object and defines an interface that conforms to component's interface. In simple words, it has a component object to be decorated.  ConcreteDecorator add responsibilities to the component object.
  • 22. Advantages of Decorator Design Pattern It is flexible than inheritance because inheritance adds responsibility at compile time but decorator pattern adds at run time. We can have any number of decorators and also in any order. It extends functionality of object without affecting any other object.
  • 23. Disadvantage of Decorator Design Pattern The main disadvantage of decorator design pattern is code maintainability because this pattern creates lots of similar decorators which are sometimes hard to maintain and distinguish.