SlideShare a Scribd company logo
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Decorator Pattern
Adeel Riaz
Muhammad Shehyyar
Muhammad Owais
Sara Rehmatullah
Ayesha Mehfooz
Advisor: Muhammad Qasim Pasta
PAF KIET Fall 11PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Concept of Decorating in Real World
Before Decorating
After Decorating
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Problem??
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Decorator Pattern Approach
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Problem??
PAF-KIET CoCIS Dept.
Whenever a team member becomes a team lead, we have to create a new
object of team lead and the previous object that points to that employee (team
member) may be destroyed.
Another case is when an employee can perform responsibilities of a team member as well as those of a team
lead or a manager can perform team leads responsibilities. In that case you need to create two objects for the
same employee which is totally wrong.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Decorator Pattern Approach
PAF-KIET CoCIS Dept.
Now, if we want to change responsibilities of an employee to manager we just need a new
Manager (Decorator) and assigning that employee to it will solve our problem. Same is the case
when a team lead’s responsibilities are revoked, and some other member becomes team lead,
we just need to swap employee objects within TeamMember and TeamLead decorators.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Intent
The Decorator Pattern attaches additional
responsibilities to an object dynamically.
Decorators provide a flexible alternative to
Sub-classing for extending functionality.
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Motivation
• Sometimes we want to add responsibilities to individual
objects not to an entire class.
• Inheriting responsibilities from another class attaches
them to every subclass instance statically. This is
inflexible.
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Graphical user interface toolkit
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Structure
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Example
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Applicability
PAF-KIET CoCIS Dept.
•To add responsibilities to individual objects dynamically
and transparently, that is, without affecting other objects.
•For responsibilities that can be withdrawn.
•When extension by sub-classing is impractical.
Sometimes a large number of independent extensions
are possible and would produce an explosion of
subclasses to support every combination. Or a class
definition may be hidden or otherwise unavailable for
sub-classing.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Participants
• Component (VisualComponent)
defines the interface for objects that can have responsibilities added
to them dynamically.
• ConcreteComponent (TextView)
defines an object to which additional responsibilities can be
attached.
• Decorator
maintains a reference to a Component object and defines an
interface that conforms to Component‘s interface.
• ConcreteDecorator (BorderDecorator, ScrollDecorator)
adds responsibilities to the component.
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Participants
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Collaborations
PAF-KIET CoCIS Dept.
•Decorator forwards requests to its Component object. It may
optionally perform additional operations before and after forwarding
the request.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Consequences
• More flexible than static inheritance.
• Avoids feature laden classes high up in
hierarchy.
• Lots of little objects that look alike. So it is
hard to learn and debug.
• A decorator and its components are not
identical. So checking object identification can
cause problems.
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Implementation
Several issues should be considered when applying the Decorator pattern:
1. Interface conformance:
A decorator object’s interface must conform to the interface of the
component it decorates.
2. Omitting the abstract Decorator class:
If only one responsibility is needed, don’t define abstract Decorator.
Merge Decorator’s responsibility into the ConcreteDecorator.
3. Keeping Component classes light weight:
Component class should be dedicated to defining an interface, no other
functions. Keep it light and simple. A complex Component class might make
Decorator too costly to use in quantity.
4. Changing the skin of an object versus its guts:
Decorator classes should act as a layer of skin over an object. If there’s a need
to change the object’s guts, use Strategy pattern.
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Builder VS Decorator
PAF-KIET CoCIS Dept.
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
PAF-KIET CoCIS Dept.
Builder VS Decorator
• Click to edit Master text styles
– Second level
• Third level
– Fourth level
» Fifth level
Related Patterns
PAF-KIET CoCIS Dept.
•Adapter: A decorator is different from an adapter in that a
decorator only changes an object‘s responsibilities, not its
interface; an adapter will give an object a completely new
interface.
•Strategy : A decorator lets you change the skin of an object; a
strategy lets you change the guts. These are two alternative
ways of changing an object.

More Related Content

PPTX
The Singleton Pattern Presentation
PPTX
Design Pattern - Singleton Pattern
PPT
Flyweight pattern
ODP
Visitor pattern
PPTX
Factory Method Pattern
PPTX
Design pattern (Abstract Factory & Singleton)
PDF
Design Patterns - Factory Method & Abstract Factory
PPSX
Proxy design pattern
The Singleton Pattern Presentation
Design Pattern - Singleton Pattern
Flyweight pattern
Visitor pattern
Factory Method Pattern
Design pattern (Abstract Factory & Singleton)
Design Patterns - Factory Method & Abstract Factory
Proxy design pattern

What's hot (20)

PPTX
Design Pattern - Factory Method Pattern
PPT
Prototype pattern
PDF
Design patterns
PDF
Spring Security
PPTX
Decorator Pattern
PPTX
Observer Software Design Pattern
PPTX
Gof design patterns
PPTX
SOLID principles
PPTX
Visitor Pattern
PPT
Adapter pattern
PPTX
Strategy Pattern
PDF
Design Patterns Presentation - Chetan Gole
PPT
Bridge Design Pattern
PDF
Selenium IDE LOCATORS
PPTX
Adapter Design Pattern
PPTX
Publish Subscribe pattern - Design Patterns
PPTX
Dependency injection - the right way
PPTX
Functional Application Logging : Code Examples Using Spring Boot and Logback
PDF
Java Course 11: Design Patterns
PPTX
Design Pattern - Observer Pattern
Design Pattern - Factory Method Pattern
Prototype pattern
Design patterns
Spring Security
Decorator Pattern
Observer Software Design Pattern
Gof design patterns
SOLID principles
Visitor Pattern
Adapter pattern
Strategy Pattern
Design Patterns Presentation - Chetan Gole
Bridge Design Pattern
Selenium IDE LOCATORS
Adapter Design Pattern
Publish Subscribe pattern - Design Patterns
Dependency injection - the right way
Functional Application Logging : Code Examples Using Spring Boot and Logback
Java Course 11: Design Patterns
Design Pattern - Observer Pattern
Ad

Similar to Decorator Design Pattern (20)

PPTX
Decorator design pattern
PPTX
Decorator Pattern 2.pptxdsadsadsadsadasdasdsa
PPTX
Decorator pattern
PDF
Decorator Pattern | Object Oriented Design Pattern
PPTX
Design Pattern(decorator design pattern) and anti pattern
PDF
Structure-Decorator deign pattern model fact
PPT
Stoop 439-decorator
PPT
Design patterns structuralpatterns(thedecoratorpattern)
 
PPTX
Decorator Pattern presentation theory and examples
PPTX
Design pattern reading group – decorator pattern
PPTX
Decorator design pattern
PPTX
Design Patterns - 01 Introduction and Decorator Pattern
PDF
Decorator design pattern (A Gift Wrapper)
PPTX
java training in chennai
PPT
Design patterns - Decorator pattern
PPT
Phani Kumar - Decorator Pattern
PDF
DECORATOR PATTERN IN WEB APPLICATION Design
PDF
DECORATOR PATTERN IN WEB APPLICATION
PDF
Presentation on design pattern software project lll
PPT
10-DesignPatterns.ppt
Decorator design pattern
Decorator Pattern 2.pptxdsadsadsadsadasdasdsa
Decorator pattern
Decorator Pattern | Object Oriented Design Pattern
Design Pattern(decorator design pattern) and anti pattern
Structure-Decorator deign pattern model fact
Stoop 439-decorator
Design patterns structuralpatterns(thedecoratorpattern)
 
Decorator Pattern presentation theory and examples
Design pattern reading group – decorator pattern
Decorator design pattern
Design Patterns - 01 Introduction and Decorator Pattern
Decorator design pattern (A Gift Wrapper)
java training in chennai
Design patterns - Decorator pattern
Phani Kumar - Decorator Pattern
DECORATOR PATTERN IN WEB APPLICATION Design
DECORATOR PATTERN IN WEB APPLICATION
Presentation on design pattern software project lll
10-DesignPatterns.ppt
Ad

Recently uploaded (20)

PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Patient Appointment Booking in Odoo with online payment
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PPTX
assetexplorer- product-overview - presentation
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PDF
Cost to Outsource Software Development in 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Complete Guide to Website Development in Malaysia for SMEs
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
17 Powerful Integrations Your Next-Gen MLM Software Needs
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Salesforce Agentforce AI Implementation.pdf
Autodesk AutoCAD Crack Free Download 2025
Patient Appointment Booking in Odoo with online payment
Advanced SystemCare Ultimate Crack + Portable (2025)
assetexplorer- product-overview - presentation
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Cost to Outsource Software Development in 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Designing Intelligence for the Shop Floor.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Operating system designcfffgfgggggggvggggggggg
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Complete Guide to Website Development in Malaysia for SMEs

Decorator Design Pattern

  • 1. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Decorator Pattern Adeel Riaz Muhammad Shehyyar Muhammad Owais Sara Rehmatullah Ayesha Mehfooz Advisor: Muhammad Qasim Pasta PAF KIET Fall 11PAF-KIET CoCIS Dept.
  • 2. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Concept of Decorating in Real World Before Decorating After Decorating PAF-KIET CoCIS Dept.
  • 3. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Problem?? PAF-KIET CoCIS Dept.
  • 4. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Decorator Pattern Approach PAF-KIET CoCIS Dept.
  • 5. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Problem?? PAF-KIET CoCIS Dept. Whenever a team member becomes a team lead, we have to create a new object of team lead and the previous object that points to that employee (team member) may be destroyed. Another case is when an employee can perform responsibilities of a team member as well as those of a team lead or a manager can perform team leads responsibilities. In that case you need to create two objects for the same employee which is totally wrong.
  • 6. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Decorator Pattern Approach PAF-KIET CoCIS Dept. Now, if we want to change responsibilities of an employee to manager we just need a new Manager (Decorator) and assigning that employee to it will solve our problem. Same is the case when a team lead’s responsibilities are revoked, and some other member becomes team lead, we just need to swap employee objects within TeamMember and TeamLead decorators.
  • 7. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Intent The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to Sub-classing for extending functionality. PAF-KIET CoCIS Dept.
  • 8. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Motivation • Sometimes we want to add responsibilities to individual objects not to an entire class. • Inheriting responsibilities from another class attaches them to every subclass instance statically. This is inflexible. PAF-KIET CoCIS Dept.
  • 9. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Graphical user interface toolkit PAF-KIET CoCIS Dept.
  • 10. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Structure PAF-KIET CoCIS Dept.
  • 11. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Example PAF-KIET CoCIS Dept.
  • 12. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Applicability PAF-KIET CoCIS Dept. •To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects. •For responsibilities that can be withdrawn. •When extension by sub-classing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for sub-classing.
  • 13. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Participants • Component (VisualComponent) defines the interface for objects that can have responsibilities added to them dynamically. • ConcreteComponent (TextView) defines an object to which additional responsibilities can be attached. • Decorator maintains a reference to a Component object and defines an interface that conforms to Component‘s interface. • ConcreteDecorator (BorderDecorator, ScrollDecorator) adds responsibilities to the component. PAF-KIET CoCIS Dept.
  • 14. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Participants PAF-KIET CoCIS Dept.
  • 15. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Collaborations PAF-KIET CoCIS Dept. •Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.
  • 16. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Consequences • More flexible than static inheritance. • Avoids feature laden classes high up in hierarchy. • Lots of little objects that look alike. So it is hard to learn and debug. • A decorator and its components are not identical. So checking object identification can cause problems. PAF-KIET CoCIS Dept.
  • 17. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Implementation Several issues should be considered when applying the Decorator pattern: 1. Interface conformance: A decorator object’s interface must conform to the interface of the component it decorates. 2. Omitting the abstract Decorator class: If only one responsibility is needed, don’t define abstract Decorator. Merge Decorator’s responsibility into the ConcreteDecorator. 3. Keeping Component classes light weight: Component class should be dedicated to defining an interface, no other functions. Keep it light and simple. A complex Component class might make Decorator too costly to use in quantity. 4. Changing the skin of an object versus its guts: Decorator classes should act as a layer of skin over an object. If there’s a need to change the object’s guts, use Strategy pattern. PAF-KIET CoCIS Dept.
  • 18. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Builder VS Decorator PAF-KIET CoCIS Dept.
  • 19. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level PAF-KIET CoCIS Dept. Builder VS Decorator
  • 20. • Click to edit Master text styles – Second level • Third level – Fourth level » Fifth level Related Patterns PAF-KIET CoCIS Dept. •Adapter: A decorator is different from an adapter in that a decorator only changes an object‘s responsibilities, not its interface; an adapter will give an object a completely new interface. •Strategy : A decorator lets you change the skin of an object; a strategy lets you change the guts. These are two alternative ways of changing an object.