Structural Design Patterns: Decorator - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Structural Design Patterns: Decorator

Description:

Decorators work behind the scenes, they are transparent to the interface. 2004/03/16 ... The decorator class is transparent to component's clients ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 26
Provided by: Ibr77
Category:

less

Transcript and Presenter's Notes

Title: Structural Design Patterns: Decorator


1
Structural Design Patterns Decorator
  • Ibrahim Jadalowen
  • SENG 609.04, University of Calgary
  • March 16, 2004

2
Decorator Pattern
  • Agenda
  • Introduction
  • Motivation
  • Applicability
  • Structure
  • Participants
  • Example
  • Sample Code
  • Implementation issues
  • Known Uses
  • Related Patterns
  • Consequences

3
Introduction
  • Decorators expand the functionality of an
    instance of a class without changing the class
    code
  • More flexibility than static inheritance
  • Adding items such as scroll bars as needed
    provides more flexibility than requiring all
    windows to have scroll bars
  • Decorators work behind the scenes, they are
    transparent to the interface

4
Motivation (1/2)
  • Inheriting responsibilities from another class
    attaches them to every subclass instance
    statically
  • This is inflexible
  • Sometimes we want to add responsibilities to
    individual objects not to an entire class GoF95

5
Motivation (2/2)
  • A flexible approach is to enclose the component
    in another object that adds the functionality.
    The object is the decorator.
  • Decorator adds functionality to objects
    dynamically
  • The decorator class is transparent to components
    clients
  • Transparency allows an unlimited number of added
    responsibilities

6
Applicability
  • Use Decorator
  • 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 a class definition may be hidden
    or otherwise unavailable for subclassing.
  • For responsibilities that can be withdrawn
  • GoF95

7
Structure
  • Kremer,2004

8
Participants
  • Component
  • Define the interface for objects that can have
    responsibilities added to them dynamically.
  • ConcreteComponent
  • 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 Components
    interface.
  • ConcreteDecorator
  • Adds responsibilities to the component.
  • GoF95

9
Example 1
AGCS,2001
10
Text View Example
  • A TextView has the following features
  • Side scroll bar
  • Bottom scroll bar
  • Flat border
  • 3D border
  • SDSU,2001

11
Text View Example
  • This gives us 12 different options
  • TextView
  • TextViewWithNoBorderSideScrollbar
  • TextViewWithNoBorderBottomScrollbar
  • TextViewWithNoBorderBottomSideScrollbar
  • TextViewWith3DBorder
  • TextViewWith3DBorderSideScrollbar
  • TextViewWith3DBorderBottomScrollbar
  • TextViewWith3DBorderBottomSideScrollbar
  • TextViewWithFlatBorder
  • TextViewWithFlatBorderSideScrollbar
  • TextViewWithFlatBorderBottomScrollbar
  • TextViewWithFlatBorderBottomSideScrollbar
  • SDSU,2001

12
Text View Example
  • Solution 1 - Use Object Composition

SDSU,2001
13
Text View Example
  • class TextView
  • Border myBorder
  • ScrollBar verticalBar
  • ScrollBar horizontalBar
  • public void draw()
  • myBorder.draw()
  • verticalBar.draw()
  • horizontalBar.draw()
  • //other textView code
  • But TextView knows about all the variations!
  • New type of variations require changing TextView
    (and any other type of view we have)
  • SDSU,2001

14
Text View Example
  • Solution 2 Use Decorator
  • Change the skin of an object not its guts
  • TextView has no borders or scrollbars!
  • Add borders and scrollbars on top of a TextView
  • Runtime
  • Structure ?
  • SDSU, 2001

15
Decorator Sample Code
  • Now the TextView class knows nothing about
    borders and scrollbars
  • class TextView
  • Public
  • void draw()
  • // Code to draw the TextView object itself.

16
Decorator Sample Code
  • But the decorators need to know about the
    components
  • Class VisualDecorator public VisualComponent
  • Private
  • VisualComponent vComp
  • public
  • VisualDecorator(VisualComponent)
  • virtual void Draw()
  • vComp-gtDraw()
  • //

17
Decorator Sample Code
  • class 3DBorder public VisualDecorator
  • private
  • int width
  • void DrawBorder(int)
  • public
  • 3DBorder(VisualComponent, int w)
  • virtual void Draw()
  • Void 3DBorderDraw ()
  • VisualDecoratorDraw()
  • DrawBorder(w)

18
Decorator Sample Code
  • Here we use decorators to crate a bordered
    scrollable TextView
  • void main(char args)
  • TextView TV new TextView()
  • ScrollDecorator SD new ScrollDecorator(TV)
  • VisualComponent bstext new 3DBorder(SD)
  • bstext.draw()
  • //OR
  • VisualComponent bstext
  • new 3DBorder(
  • new ScrollDecorator( new TextView() ))
  • bstext.draw()

19
Implementation Issues
  • Several issues should be considered when applying
    the Decorator pattern
  • 1. Interface conformance
  • A decorator objects interface must conform to
    the interface of the component it decorates.
  • 2. Omitting the abstract Decorator class
  • If only one responsibility is needed, dont
    define abstract Decorator. Merge Decorators
    responsibility into the ConcreteDecorator.

20
Implementation Issues
  • 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 theres a need to change the
    objects guts, use Strategy pattern.

21
Known Uses
  • Commonly used in basic system frameworks
    Windows, streams, fonts
  • Interface Tools InterViews, ET,
    ObjectWorks\Smalltalk class library
  • DebuggingGlyph from IneterViews
  • PassivityWrapper from ParcPlace Smalltalk

22
Related Patterns
  • Strategy In Strategy the guts of the object are
    changed, In Decorator only the skin is changed.
  • Composite It is often used with decorator. A
    decorator has one component and adds additional
    responsibilities. Composite uses aggregate
    composition of objects.
  • Adapter A decorators adds responsibilities, an
    Adapter changes the object interface

23
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

24
Other Pros Cons
  • Pros
  • Can add responsibility to one object without
    affecting the other class objects
  • When adding, no need to know the interface of the
    class
  • Decorator can easily attach and detach
    functionality to objects.
  • Cons
  • If Decorator is complex, it becomes costly to use
    in quantity
  • Always need to maintain the interface to keep in
    sync with the object

25
References
  • GoF95 Erich Gamma, Richard Helm, Ralph
    Johnson, John Vlissides. Design Patterns
    Elements of Reusable Object-Oriented Software.
    Addison-Wesley Professional Computing Series,
    Addison-Wesley, Reading Mass. 1995.
  • AGCS AG Communication Systems,
    http//www.agcs.com/
  • SDSU Roger Whitney (2001) CS 635 Advanced
    Object-Oriented Design Programming, Decorator,
    Chain of Responsibility, OO Recursion, San Diego
    State University, http//www.eli.sdsu.edu/courses/
    spring01/cs635/notes/decorator/decorator.html
  • Bob Tarr, Computer Science and Electrical
    Engineering Department, University of Maryland
    Baltimore County, http//www.research.umbc.edu/ta
    rr/dp/lectures/Decorator-2pp.pdf
Write a Comment
User Comments (0)
About PowerShow.com