Decorator Industry - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Decorator Industry

Description:

A decorator pattern is composed of a lot of little objects, that make it really hard to debug. ... http://selab.korea.ac.kr/selab/courses/GoF-patterns/decorator.htm ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 25
Provided by: Luo91
Category:

less

Transcript and Presenter's Notes

Title: Decorator Industry


1
Decorator Industry
  • Decorator Pattern
  • Presentation
  • Samuel Lee
  • George Luo

2
Decorator Pattern
  • Table of content
  • Introduction
  • Structure
  • Motivation
  • Applicability
  • Implementation
  • Example
  • Advantage Disadvantage
  • Know uses
  • Reference

3
Introduction
  • Attaching additional functionality or
    responsibility to an object dynamically.
  • Provides a flexible alternative to inheritance
    (which is static and applies to entire class) for
    extending functionality.
  • Decorators may be chained together dynamically,
    so that each performs some new functions.
  • Also know as Wrapper

4
Structure
Source from Dr. Kremers course web
5
Structure (Cont.)
  • Component
  • Define the interface for objects that can have
    responsibilities added to them dynamically.
  • Concrete Component
  • Defines an object to which additional
    responsibilities can be attached.
  • Decorator
  • Maintains a reference to Component object and
    defines an interface that conforms to Components
    interface.
  • Concrete Decorator
  • Adds responsibilities to the component.

6
Structure (Cont.)
(Source from www.agcs.com )
7
Motivation
  • One way to add responsibility is inheritance, But
    its inflexible and the additional responsibility
    is made statically.
  • Sometimes we want to add responsibilities to
    individual objects, not to entire class.

8
Motivation (Cont.)
  • A more flexible way is to enclose the component
    in another object that adds the functionality.
  • The enclosing object is called a decorator.
  • Decorator conforms to the components interface
    and the decoration process is transparent to the
    client.
  • The transparency lets you nest decorators
    recursively, thereby adding unlimited number of
    responsibilities.
  • The TextView example
  • window.addComponent(new BorderDecorator(new
    ScrollDecorator(new TextView())))

9
Implementation
  • A text view has the following features
  • side scroll bar
  • Bottom scroll bar
  • 3D border
  • Flat border
  • This gives 12 different options
  • TextView
  • TextViewWithNoBorderSideScrollbar
  • TextViewWithNoBorderBottomScrollbar
  • TextViewWithNoBorderBottomSideScrollbar
  • TextViewWith3DBorder
  • TextViewWith3DBorderSideScrollbar
  • TextViewWith3DBorderBottomScrollbar
  • TextViewWith3DBorderBottomSideScrollbar
  • TextViewWithFlatBorder
  • TextViewWithFlatBorderSideScrollbar
  • TextViewWithFlatBorderBottomScrollbar
  • TextViewWithFlatBorderBottomSideScrollbar

SDSU Roger Whitney
10
Implementation (Cont.)
  • Solution 1 - Use Object Composition

class TextView Border aBorder ScrollBar
verticalBar ScrollBar horizontalBar public
void draw() aBorder.draw()
verticalBar.draw() horizontalBar.draw()
code to draw self etc. But TextView
knows about all the variations! New type of
variations require changing TextView (and any
other type of view we have)
SDSU Roger Whitney
11
Implementation (Cont.)
  • Solution 2 - Use Decorator (change the skin of an
    object not its guts)

Runtime Structure
TextView has no borders or scrollbars! Add
borders and scrollbars on top of a TextView
SDSU Roger Whitney
12
Implementation (Cont.)
  • Things to consider for the implementation
  • Interface conformance
  • Decorator object must conform to the interfaces
    of the component it decorates.
  • Omitting the abstract Decorator class
  • When you only need to add one responsibility, can
    merge the responsibility right into the concrete
    decorator

13
Implementation (Cont.)
  • Keeping Component class lightweight
  • Base Component class should focus on interface
    not on storing data or else might make decorators
    too heavyweight
  • Too many functionalities in the Component class
    will increases the probability of paying for
    features that they dont need.
  • Changing the skin of an object versus changing
    its guts
  • Strategy are a better choice when the Component
    class is intrinsically heavyweight.
  • Decorators are transparent to the component.
  • Strategy can have its own specialized interface.

14
Implementation(Cont.)
15
Example
  • Java Code Example
  • abstract class VisualComponent( )
  • public void Draw( )
  • public void Resize( )
  • // class VisualComponent
  • abstract class Decorator extends VisualComponent
  • private VisualComponent component
  • public Decorator (VisualComponent comp)
  • this.component comp
  • // constructor
  • public Draw( )
  • component.Draw( )
  • // Draw
  • public Resize( )
  • component.resize( )
  • // Resize
  • ...

John Kuhl
16
Example (Cont.)
  • class BorderDecorator extends Decorator
  • private int width
  • public BorderDecorator(VisualComponent comp, int
    BorderWidth)
  • super(comp) // call constructor of parent
    class
  • this.width BorderWidth
  • // Constructor
  • private void DrawBorder
  • // code to implement Draw Border method
  • // DrawBorder
  • public Draw( )
  • super.Draw( )
  • DrawBorder(width)
  • // Draw

John Kuhl
17
Example (Cont.)
  • class ScrollDecorator extends Decorator
  • private int position 0
  • public ScrollDecorator(VisualComponent comp)
  • super(comp) // call constructor of parent
    class
  • // Constructor
  • private void ScrollTo(int newposition)
  • // code to implement ScrollTo method
  • // ScrollTo
  • private void DrawScroll( )
  • // code to implement DrawScroll method
  • // DrawScroll
  • public Draw( )
  • super.Draw( )
  • DrawScroll
  • // Draw
  • ..

John Kuhl
18
Example (Cont.)
  • public class Client
  • public static void main(String args)
  • TextView text new TextView()
  • ScrollDecorator stext new ScrollDecorator(text
    )
  • VisualComponent bstext new BorderDecorator(ste
    xt)
  • bstext.draw()
  • Or
  • public class Client
  • public static void main(String args)
  • VisualComponent bstext new BorderDecorator(new
    ScrollDecorator (new TextView()))
  • bstext.draw()

John Kuhl
19
Example (Cont.)
John Kuhl
20
Advantages
  • Decorator is more flexible and effective than
    Inheritance, an effective substitute for multiple
    inheritance.
  • You can just enclose the component in the
    specific object not to entire class that
    inheritance do.
  • While add responsibilities, you just need to know
    the interface of the class, the source is usually
    not required.
  • The responsibilities and functionalities can be
    added without affecting other objects.
  • Decorator adds objects at runtime as opposed to
    inheritance does at compile time.
  • Decorator can easily remove the added object as
    its added.

21
Shortcoming
  • A decorator pattern is composed of a lot of
    little objects, that make it really hard to
    debug.
  • Decorator may make a subject class overweight,
    when the class involved lots of data or methods
    already.
  • A decorator and its components are not identical,
    hence should not rely on object identity when use
    decorators.
  • Always need to maintain the interface to be in
    sync with the object.

22
Known Uses
  • X Window use Decorators to add a title bar,
    scroll bars, menu bar and border to a window.
  • ET uses Decorators to filter communication
    streams, e.g. compression and unencoding.
  • ImageVision uses Decorators to process image
    regions.
  • InterViews uses a Decorator to disable an
    interface element, by trapping and discarding
    input events and to echo requests for debugging
    interface elements.
  • A Monitor is a Decorator which automatically
    locks an object during a method call. Overloading
    operator-gt can make this look like an ordinary
    pointer in C.

23
References
  • http//www.ciol.com/content/search/showARticle.asp
    ?arid26108
  • http//www.cs.colorado.edu/users/kena/classes/6448
    1501/lectures/lecture27.pdf
  • http//www.javaworld.com/javaworld/jw-12-2001/jw-1
    214-designpatterns_p.html
  • http//csis.pace.edu/bergin/patterns/strategydeco
    rator.html
  • http//www.research.umbc.edu/tarr/dp/lectures/Dec
    orator-2pp.pdf
  • http//selab.korea.ac.kr/selab/courses/GoF-pattern
    s/decorator.htm
  • SDSU Roger Whitney http//www.eli.sdsu.edu/cou
    rses/spring01/cs635/notes/decorator/decorator.html
  • John Kuhl http//www.icaen.uiowa.edu/kuhl/SoftE
    ng/Slides1.pdf

24
Decorator Pattern
  • Question?
Write a Comment
User Comments (0)
About PowerShow.com