Chapter 9. Decorator Pattern


In This Chapter

Understanding the Decorator Pattern 114

Building Reader Decorators 117

Building Visual and Commutative Decorators 128

Summary 136

The Decorator pattern enables you to apply new behavior to an object at runtime. Traditionally, many developers learn to add behavior by using inheritance rather than composition. This means that if you want to add a move() method to an existing Widget class, you'd extend Widget to define a new MovableWidget class. Or, if you want to redefine the move() method of MovableWidget so that it moves only until its fuel is used up, you could extend MovableWidget to define FuelableMovableWidget.

However, inheritance has two major drawbacks in such cases:

  • You cannot change an object's behavior at runtime. For example, a Widget is always a Widget. It isn't possible to convert a Widget to a MovableWidget or a FuelableMovableWidget when traditional inheritance is used.

  • As more permutations become available, the number of classes required becomes unwieldy. For example, if you want to extend a Widget class so that it is scalable, you might define a ScalableWidget subclass. If you want to extend the class so that it is rotatable, you might define a RotatableWidget subclass. To make a movable widget, you might define a MovableWidget subclass. However, what if you want to combine some of the behaviors? Using inheritance, you'd have to define ScalableRotatableWidget, ScalableMovableWidget, RotatableMovableWidget, and ScalableRotatableMovableWidget. Each new behavior increases the number of required classes in a factorial fashion such that after just a few behaviors, the number of classes is unmanageable.

The solution to these inheritance drawbacks is the Decorator pattern. The Decorator pattern uses composition rather than inheritance to add new behavior to an object. This means that it's possible to add behavior and change behavior at runtime. Additionally, because the Decorator pattern uses composition, it's often possible to chain together several new behaviors in a manageable fashion.

It is often useful to visualize how a pattern works. Imagine that the Decorator pattern is like a set of Russian dollsthe type of dolls that stack inside one another. Obviously, this analogy is limited, but it does illustrate the basic nesting relationship between decorator and decorated objects. The Decorator pattern starts with a base object that can be decorated. This decorated object is analogous to the innermost Russian doll. The decorator objects use composition to add behavior to the decorated object. The decorator objects are analogous to the larger Russian dolls within which you place the smaller dolls. After you stack a Russian doll in the next larger doll, you can then stack that doll in the next larger doll. So too with the Decorator pattern; you can often use a decorator to add yet more behavior to another decoratorthus treating the decorator like a decorated object. We'll look at these types and their relationships in more detail throughout the chapter.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net