SlideShare a Scribd company logo
STRUCTURAL PATTERNS-
DECORATOR DESIGN PATTERN
Dr. S. Neelima
M.Tech Ph.D CSE
Priyadarshini Institute of Science and Technology
for Women, KHAMMAM
Intent
Decorator is a structural design pattern.
Attach additional responsibilities to an object dynamically. Decorators
provide a flexible alternative to sub classing for extending functionality.
Also Known as
Wrapper
Decorator Design Pattern- Dr. S. Neelima 2
Motivation
Imagine that you’re working on a notification library which lets other
programs notify their users about important events.
The initial version of the library was based on the Notifier class that had
only a few fields, a constructor and a single send method.
Decorator Design Pattern- Dr. S. Neelima 3
Motivation…..
Decorator Design Pattern- Dr. S. Neelima 4
Decorator Design Pattern- Dr. S. Neelima 5
The client code would need to wrap a basic notifier object into a set of
decorators that match the client’s preferences. The resulting objects will
be structured as a stack.
Decorator Design Pattern- Dr. S. Neelima 6
Motivation
2.
For example, suppose we have a TextView object that displays text
in a window. TextView has no scroll bars by default, because we might not
always need them. When we do, we can use a ScrollDecorator to add
them. Suppose we also want to add a thick black border around the
TextView. We can use a BorderDecorator to add this as well. We simply
compose the decorators with the TextView to produce the desired result.
Decorator Design Pattern- Dr. S. Neelima 7
Motivation
2. Solution :
Decorator Design Pattern- Dr. S. Neelima 8
Applicability
• Use the pattern to add responsibilities to individual objects
dynamically and transparently, that is, without affecting other objects.
• Use the pattern for responsibilities that can be withdrawn.
• Use the pattern when it’s awkward or not possible to extend an object’s
behavior using inheritance.
Decorator Design Pattern- Dr. S. Neelima 9
Structure
Decorator Design Pattern- Dr. S. Neelima 10
Participants
• Component (VisualComponent)
• Concrete Component (TextView)
• Decorator
• ConcreteDecorator
Decorator Design Pattern- Dr. S. Neelima 11
Collaborations
Decorator forwards requests to its Component object. It may optionally
perform additional operations before and after forwarding the request.
Consequences
Pros
• You can extend an object’s behavior without making a new subclass.
• You can add or remove responsibilities from an object at runtime.
• You can combine several behaviors by wrapping an object into multiple
decorators.
• Single Responsibility Principle. You can divide a monolithic class that
implements many possible variants of behavior into several smaller
classes.
Cons
• It’s hard to remove a specific wrapper from the wrappers stack.
• It’s hard to implement a decorator in such a way that its behavior
doesn’t depend on the order in the decorators stack.
• The initial configuration code of layers might look pretty ugly.
Decorator Design Pattern- Dr. S. Neelima 12
Implement
• Make sure your business domain can be represented as a primary
component with multiple optional layers over it.
• Figure out what methods are common to both the primary component
and the optional layers. Create a component interface and declare those
methods there.
• Create a concrete component class and define the base behavior in it.
• Create a base decorator class. It should have a field for storing a
reference to a wrapped object. The field should be declared with the
component interface type to allow linking to concrete components as
well as decorators. The base decorator must delegate all work to the
wrapped object.
Decorator Design Pattern- Dr. S. Neelima 13
Implement ….
• Make sure all classes implement the component interface.
• Create concrete decorators by extending them from the base decorator.
A concrete decorator must execute its behavior before or after the call to
the parent method (which always delegates to the wrapped object).
• The client code must be responsible for creating decorators and
composing them in the way the client needs.
Decorator Design Pattern- Dr. S. Neelima 14
Known Uses
Decorator Design Pattern- Dr. S. Neelima 15
• InterViews ,ET++ and the ObjectWorksSmalltalk class library.
• More exotic applications of Decorator are the DebuggingGlyph from
InterViews and the PassivityWrapper from ParcPlace Smalltalk.
Related Patterns
• Adapter changes the interface of an existing object,
while Decorator enhances an object without changing its interface.
• Composite and Decorator have similar structure diagrams since both
rely on recursive composition to organize an open-ended number of
objects.
• Decorator lets you change the skin of an object, while Strategy lets
you change the guts.
Decorator Design Pattern- Dr. S. Neelima 16
Decorator Design Pattern- Dr. S. Neelima 17

More Related Content

What's hot

Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
Mindfire Solutions
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
Shakil Ahmed
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
Rana Muhammad Asif
 
Facade pattern
Facade patternFacade pattern
Facade pattern
JAINIK PATEL
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
sahilrk911
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
Jyaasa Technologies
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
Dimuthu Anuraj
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
naveen kumar
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
Aman Jain
 
Dependency injection presentation
Dependency injection presentationDependency injection presentation
Dependency injection presentation
Ahasanul Kalam Akib
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
JAINIK PATEL
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Hùng Nguyễn Huy
 
Design Pattern in Software Engineering
Design Pattern in Software EngineeringDesign Pattern in Software Engineering
Design Pattern in Software Engineering
Manish Kumar
 
Presentation facade design pattern
Presentation facade design patternPresentation facade design pattern
Presentation facade design pattern
Bayu Firmawan Paoh
 
Repository Pattern with c# and Entity Framework
Repository Pattern with c# and Entity FrameworkRepository Pattern with c# and Entity Framework
Repository Pattern with c# and Entity Framework
Imtiaj Ahammad
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
Rothana Choun
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
Andreas Enbohm
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design Patterns
Michael Heron
 
Structural patterns
Structural patternsStructural patterns
Structural patterns
Himanshu
 
Design patterns
Design patternsDesign patterns
Design patterns
abhisheksagi
 

What's hot (20)

Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Facade pattern
Facade patternFacade pattern
Facade pattern
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 
Dependency injection presentation
Dependency injection presentationDependency injection presentation
Dependency injection presentation
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Design Pattern in Software Engineering
Design Pattern in Software EngineeringDesign Pattern in Software Engineering
Design Pattern in Software Engineering
 
Presentation facade design pattern
Presentation facade design patternPresentation facade design pattern
Presentation facade design pattern
 
Repository Pattern with c# and Entity Framework
Repository Pattern with c# and Entity FrameworkRepository Pattern with c# and Entity Framework
Repository Pattern with c# and Entity Framework
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design Patterns
 
Structural patterns
Structural patternsStructural patterns
Structural patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 

Similar to Decorator design pattern

Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
stanbridge
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their application
Hiệp Tiến
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
anguraju1
 
Design Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur University
Shubham Narkhede
 
JS Design patterns in Web technologies including oop techniques.pptx
JS Design patterns in Web technologies including oop techniques.pptxJS Design patterns in Web technologies including oop techniques.pptx
JS Design patterns in Web technologies including oop techniques.pptx
husnainali397602
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
Julie Iskander
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
Sachin Patidar
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
Prageeth Sandakalum
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
Sergey Aganezov
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Satheesh Sukumaran
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - Introduction
Mudasir Qazi
 
Design patterns
Design patternsDesign patterns
Design patterns
Akhilesh Gupta
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
RushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
RushikeshChikane2
 
Sda 8
Sda   8Sda   8
80410172053.pdf
80410172053.pdf80410172053.pdf
80410172053.pdf
WrushabhShirsat3
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
MD Sulaiman
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Facade design pattern
Facade design patternFacade design pattern
Facade design pattern
Neelima Sanagavarapu
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 

Similar to Decorator design pattern (20)

Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their application
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Design Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur University
 
JS Design patterns in Web technologies including oop techniques.pptx
JS Design patterns in Web technologies including oop techniques.pptxJS Design patterns in Web technologies including oop techniques.pptx
JS Design patterns in Web technologies including oop techniques.pptx
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - Introduction
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Sda 8
Sda   8Sda   8
Sda 8
 
80410172053.pdf
80410172053.pdf80410172053.pdf
80410172053.pdf
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
Facade design pattern
Facade design patternFacade design pattern
Facade design pattern
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 

Recently uploaded

原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
Kamal Acharya
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
openshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoinopenshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoin
snaprevwdev
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
Shiny Christobel
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
Ericsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.pptEricsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.ppt
wafawafa52
 
Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...
pvpriya2
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
Dwarkadas J Sanghvi College of Engineering
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
ijseajournal
 
5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf
AlvianRamadhani5
 
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
DharmaBanothu
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
Indrajeet sahu
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 

Recently uploaded (20)

原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
openshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoinopenshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoin
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
Ericsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.pptEricsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.ppt
 
Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
 
5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf
 
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 

Decorator design pattern

  • 1. STRUCTURAL PATTERNS- DECORATOR DESIGN PATTERN Dr. S. Neelima M.Tech Ph.D CSE Priyadarshini Institute of Science and Technology for Women, KHAMMAM
  • 2. Intent Decorator is a structural design pattern. Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing for extending functionality. Also Known as Wrapper Decorator Design Pattern- Dr. S. Neelima 2
  • 3. Motivation Imagine that you’re working on a notification library which lets other programs notify their users about important events. The initial version of the library was based on the Notifier class that had only a few fields, a constructor and a single send method. Decorator Design Pattern- Dr. S. Neelima 3
  • 5. Decorator Design Pattern- Dr. S. Neelima 5
  • 6. The client code would need to wrap a basic notifier object into a set of decorators that match the client’s preferences. The resulting objects will be structured as a stack. Decorator Design Pattern- Dr. S. Neelima 6
  • 7. Motivation 2. For example, suppose we have a TextView object that displays text in a window. TextView has no scroll bars by default, because we might not always need them. When we do, we can use a ScrollDecorator to add them. Suppose we also want to add a thick black border around the TextView. We can use a BorderDecorator to add this as well. We simply compose the decorators with the TextView to produce the desired result. Decorator Design Pattern- Dr. S. Neelima 7
  • 8. Motivation 2. Solution : Decorator Design Pattern- Dr. S. Neelima 8
  • 9. Applicability • Use the pattern to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects. • Use the pattern for responsibilities that can be withdrawn. • Use the pattern when it’s awkward or not possible to extend an object’s behavior using inheritance. Decorator Design Pattern- Dr. S. Neelima 9
  • 11. Participants • Component (VisualComponent) • Concrete Component (TextView) • Decorator • ConcreteDecorator Decorator Design Pattern- Dr. S. Neelima 11 Collaborations Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.
  • 12. Consequences Pros • You can extend an object’s behavior without making a new subclass. • You can add or remove responsibilities from an object at runtime. • You can combine several behaviors by wrapping an object into multiple decorators. • Single Responsibility Principle. You can divide a monolithic class that implements many possible variants of behavior into several smaller classes. Cons • It’s hard to remove a specific wrapper from the wrappers stack. • It’s hard to implement a decorator in such a way that its behavior doesn’t depend on the order in the decorators stack. • The initial configuration code of layers might look pretty ugly. Decorator Design Pattern- Dr. S. Neelima 12
  • 13. Implement • Make sure your business domain can be represented as a primary component with multiple optional layers over it. • Figure out what methods are common to both the primary component and the optional layers. Create a component interface and declare those methods there. • Create a concrete component class and define the base behavior in it. • Create a base decorator class. It should have a field for storing a reference to a wrapped object. The field should be declared with the component interface type to allow linking to concrete components as well as decorators. The base decorator must delegate all work to the wrapped object. Decorator Design Pattern- Dr. S. Neelima 13
  • 14. Implement …. • Make sure all classes implement the component interface. • Create concrete decorators by extending them from the base decorator. A concrete decorator must execute its behavior before or after the call to the parent method (which always delegates to the wrapped object). • The client code must be responsible for creating decorators and composing them in the way the client needs. Decorator Design Pattern- Dr. S. Neelima 14
  • 15. Known Uses Decorator Design Pattern- Dr. S. Neelima 15 • InterViews ,ET++ and the ObjectWorksSmalltalk class library. • More exotic applications of Decorator are the DebuggingGlyph from InterViews and the PassivityWrapper from ParcPlace Smalltalk.
  • 16. Related Patterns • Adapter changes the interface of an existing object, while Decorator enhances an object without changing its interface. • Composite and Decorator have similar structure diagrams since both rely on recursive composition to organize an open-ended number of objects. • Decorator lets you change the skin of an object, while Strategy lets you change the guts. Decorator Design Pattern- Dr. S. Neelima 16
  • 17. Decorator Design Pattern- Dr. S. Neelima 17