130 | Chapter 4: Decorator Pattern
Key Features
We can understand the Decorator pattern in terms of two key features. Often, devel-
opers want to add unique responsibilities for an object without adding those same
responsibilities to the whole class. Among other design patterns, the Decorator pat-
tern’s characterized by adding unique responsibilities. The identifying characteristic
of the Decorator pattern is to add responsibilities in a uniquely Decorator fashion.
Wrapping a component in an object that adds a responsibility follows a couple of
guidelines:
- Decorators can appear wherever a component object can.
- At runtime, you can mix and match combinations of decorators as needed.
To understand the Decorator design pattern’s key features, you need to consider
some alternatives to implementing the work the pattern does. Essentially, your
project requires that you add new features and responsibilities to individualobjects
rather than the entireclass. To do so using inheritance would bloat the class and
change the structure with each new feature. Every single object would inherit all the
features and functionality of every other object, and that’s not what you want.
In this chapter, you will see the term “component” a good deal. The
reference to component here is wholly unrelated to the components in
Flash, used for UIs, Media, Data and other purposes. In the context of
this chapter, a component refers to a concrete instance that is deco-
rated with another concrete instance called adecorator. So, for the
time being, don’t think of components as anything other than some-
thing that gets decorated. (In the last application example of a Decora-
tor design pattern in this chapter, you’ll be using Flash UI
components, but by then you’ll be able to distinguish the different
kind of components.)
Imagine you’re setting up an automobile dealership site. You can choose between
different models of autos and add different features—options. You can set up
options such as an MP3 player, Global Positioning System (GPS), cloth, vinyl or
leather seat covers, and different kinds of alarm systems. If you use inheritance, every
one of those options would have to be in every object. What’s more, you’d need to
have all the models in your main class as well. That’s absurd! Why would anyone
need both cloth and vinyl seats or be both a Ford Escape and a Chevrolet Malibu?
Then, if a new option were introduced, you’d have to bloat the class with yet another
option for every single object. However, if you can just wrap a single responsibility
around a component when and if you need it, you can keep your program slim, trim
and ready to adapt.
The key to understanding the Decorator design pattern is to understand that it uses
inheritance and employsabstract classes; however, as you know from Chapter 2,
ActionScript 3.0 doesn’t support abstract classes. Youcancreate classes that work