SlideShare a Scribd company logo
2
Most read
3
Most read
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

PPT
3. challenges
PPT
OODM-object oriented data model
PPT
Object Oriented Programming Concepts
PDF
Introduction to Design Pattern
PPTX
Object relational database management system
PPTX
Map Reduce
PDF
Difference between star schema and snowflake schema
PPTX
Decorator Pattern
3. challenges
OODM-object oriented data model
Object Oriented Programming Concepts
Introduction to Design Pattern
Object relational database management system
Map Reduce
Difference between star schema and snowflake schema
Decorator Pattern

What's hot (20)

PPT
3.7 outlier analysis
PPTX
Issues in knowledge representation
PDF
CS8592-OOAD Lecture Notes Unit-3
PPTX
Composite design pattern
PPTX
Knowledge Discovery and Data Mining
PPTX
Design pattern-presentation
PPTX
Database systems - Chapter 2
PPTX
Data science.chapter-1,2,3
PPT
Lecture 01 introduction to database
PPT
Fundamentals of Database system
PDF
Dbms 3: 3 Schema Architecture
PDF
Introduction to oops concepts
PPT
Chapter10 conceptual data modeling
PPT
Unit-3_BDA.ppt
PDF
Comparison of Relational Database and Object Oriented Database
PPT
Introduction to Expert Systems {Artificial Intelligence}
PPTX
Chapter 1 big data
PPTX
Computer Vision
PDF
1. GRID COMPUTING
PDF
Android intents
3.7 outlier analysis
Issues in knowledge representation
CS8592-OOAD Lecture Notes Unit-3
Composite design pattern
Knowledge Discovery and Data Mining
Design pattern-presentation
Database systems - Chapter 2
Data science.chapter-1,2,3
Lecture 01 introduction to database
Fundamentals of Database system
Dbms 3: 3 Schema Architecture
Introduction to oops concepts
Chapter10 conceptual data modeling
Unit-3_BDA.ppt
Comparison of Relational Database and Object Oriented Database
Introduction to Expert Systems {Artificial Intelligence}
Chapter 1 big data
Computer Vision
1. GRID COMPUTING
Android intents
Ad

Similar to Decorator design pattern (20)

PPTX
Cs 1023 lec 8 design pattern (week 2)
PPTX
Design pattern and their application
PPTX
UNIT IV DESIGN PATTERNS.pptx
DOCX
Design Pattern Notes: Nagpur University
PPTX
JS Design patterns in Web technologies including oop techniques.pptx
PPTX
Design Pattern lecture 2
PDF
10 Modeling Object and Classes.pdf Software
PPTX
Creational Design Patterns.pptx
PPTX
Introduction to Design Patterns
PDF
Software Design Patterns. Part I :: Structural Patterns
PPT
Software Design Patterns
PPT
Software Design Patterns
PPTX
Design Pattern - Introduction
PPTX
Design patterns
PPT
Chapter 4_Introduction to Patterns.ppt
PPT
Chapter 4_Introduction to Patterns.ppt
PPTX
PDF
80410172053.pdf
PPTX
Fundamentals of OOP (Object Oriented Programming)
PPTX
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
Cs 1023 lec 8 design pattern (week 2)
Design pattern and their application
UNIT IV DESIGN PATTERNS.pptx
Design Pattern Notes: Nagpur University
JS Design patterns in Web technologies including oop techniques.pptx
Design Pattern lecture 2
10 Modeling Object and Classes.pdf Software
Creational Design Patterns.pptx
Introduction to Design Patterns
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns
Software Design Patterns
Design Pattern - Introduction
Design patterns
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
80410172053.pdf
Fundamentals of OOP (Object Oriented Programming)
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
Ad

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PPT
Mechanical Engineering MATERIALS Selection
PPT
introduction to datamining and warehousing
PPTX
Geodesy 1.pptx...............................................
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
PPT on Performance Review to get promotions
PPT
Project quality management in manufacturing
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
737-MAX_SRG.pdf student reference guides
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT
Total quality management ppt for engineering students
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Artificial Intelligence
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Safety Seminar civil to be ensured for safe working.
Mechanical Engineering MATERIALS Selection
introduction to datamining and warehousing
Geodesy 1.pptx...............................................
CYBER-CRIMES AND SECURITY A guide to understanding
PPT on Performance Review to get promotions
Project quality management in manufacturing
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
UNIT-1 - COAL BASED THERMAL POWER PLANTS
737-MAX_SRG.pdf student reference guides
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Total quality management ppt for engineering students
Well-logging-methods_new................
573137875-Attendance-Management-System-original
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Fundamentals of safety and accident prevention -final (1).pptx
Artificial Intelligence

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