What Is the Decorator Pattern? | 131
likeabstract classes, simply by not instantiating them directly. In fact, that’s what an
abstract class is to some extent—a class that you do not instantiate but can extend.
They work something like an interface, but while you can implement an interface
and its abstract methods, you cannot extend it. (You were introduced to using inter-
faces in Chapter 1, and in Chapter 5, you will be using a design pattern that employs
interfaces.)
Lack of support of abstract classes is a sore point with some Flash
and Fle xdevelopers. Before firing off an impassioned email to
Adobe, though, first take a look at the ECMAScript Rev 4 specs.
These specifications don’t exactly support abstract classes either, at
this point in time.
Why are abstract classes important for the Decorator pattern? Like interfaces, you
can create abstract methods that can be implemented in different ways. At the same
time, you can use them to create subclasses so that core properties can be inherited
in ways not possible with interfaces alone.
The Decorator Model
The Decorator outlined in the class diagram notation shows two key elements: com-
ponent and decorator. The component represents what’s to be decorated, and the
decorator is the abstract class for the concrete decorations. The concrete component
is what’s actually decorated, and the concrete decorations are the actual decora-
tions. All the concrete decorations and the concrete component are subclassed from
the same source. Keeping that in mind, take a look at the class diagram in Figure 4-1.
In the most basic sense, the component is the Christmas tree, and the decorations are
the ornaments. Each concrete decorator wraps the tree and is subclassed from the
same source as the tree. A better way to think about components and decorators is
Figure 4-1. Decorator design pattern
Component
Operation()
Concrete Component
Operation()
Decorator
Operation()
Concrete Decoration Alpha
Operation()
Concrete Decoration Beta
Operation()
addedState
wraps