Structural Pattern
DECORATOR
Mohammad Reda Katby
Solution Architect
MOHE
Definition
• Provide a way of attaching new state and behavior to an object dynamically.
• The object does not know it is being “decorated”, which makes this a useful
pattern for evolving systems.
• A key implementation point in the Decorator pattern is that decorators both
inherit the original class and contain an instantiation of it.
Description
The importance of this pattern is that:
• The original object is unaware of any decorations.
• There is no one big feature-laden class with all the options in it.
• The decorations are independent of each other.
• The decorations can be composed together in a mix-and-match fashion.
Design
• Component
• An original class of objects that can have operations added or modified (there may be more than one such
class)
• Operation
• An operation in IComponent objects that can be replaced (there may be several operations)
• IComponent
• The interface that identifies the classes of objects that can be decorated (Component is one of these)
• Decorator
• A class that conforms to the IComponent interface and adds state and/or behavior (there may be more than
one such class)
Design
Quiz
Pattern Elements
• IComponent
• Component
• Operation
• Decorator
• Client
Example Elements
• Any photo
• A plain photo
• To display a photo
• A tagged photo
• Creator of photos and tagged photos
Options
• Multiple components
• Different components that conform to the interface
• Multiple decorators
• Multiple operations
Use
• Fits well in the graphics world
• Used in System.IO.Stream
• Used in Screen Creation inWPF
Use
Use the Decorator pattern when…
• You have:
• An existing component class that may be unavailable for subclassing.
• You want to:
• Attach additional state or behavior to an object dynamically.
• Make changes to some objects in a class without affecting others.
• Avoid subclassing because too many classes could result.

Decorator pattern

  • 1.
    Structural Pattern DECORATOR Mohammad RedaKatby Solution Architect MOHE
  • 2.
    Definition • Provide away of attaching new state and behavior to an object dynamically. • The object does not know it is being “decorated”, which makes this a useful pattern for evolving systems. • A key implementation point in the Decorator pattern is that decorators both inherit the original class and contain an instantiation of it.
  • 3.
    Description The importance ofthis pattern is that: • The original object is unaware of any decorations. • There is no one big feature-laden class with all the options in it. • The decorations are independent of each other. • The decorations can be composed together in a mix-and-match fashion.
  • 4.
    Design • Component • Anoriginal class of objects that can have operations added or modified (there may be more than one such class) • Operation • An operation in IComponent objects that can be replaced (there may be several operations) • IComponent • The interface that identifies the classes of objects that can be decorated (Component is one of these) • Decorator • A class that conforms to the IComponent interface and adds state and/or behavior (there may be more than one such class)
  • 5.
  • 6.
    Quiz Pattern Elements • IComponent •Component • Operation • Decorator • Client Example Elements • Any photo • A plain photo • To display a photo • A tagged photo • Creator of photos and tagged photos
  • 7.
    Options • Multiple components •Different components that conform to the interface • Multiple decorators • Multiple operations
  • 8.
    Use • Fits wellin the graphics world • Used in System.IO.Stream • Used in Screen Creation inWPF
  • 9.
    Use Use the Decoratorpattern when… • You have: • An existing component class that may be unavailable for subclassing. • You want to: • Attach additional state or behavior to an object dynamically. • Make changes to some objects in a class without affecting others. • Avoid subclassing because too many classes could result.