136 | Chapter 4: Decorator Pattern
another kind of object that can be decorated, such as a front yard to be decorated
with gnomes and pink plastic flamingoes. Additionally, you want to be able to
retrieve information, and so it has a getter function.
Abstract Decorator Class
Next, the abstract decorator is a subclass class of theComponentclass that inherits the
informationvariable, so nothing is needed as far as theinformationvariable is con-
cerned. In fact, nothing’s required for this simple example other than defining the
class as an extension of theComponentclass. However, thegetInformation()method
is re-implemented independently of theComponentclass, using theoverridestate-
ment—which does what it says on the tin; it overrides parent class methods. This is
done to distinguish the same method being used for theDecoratorclass from the
method being used for theComponentclass. All the concrete decorations are sub-
classed from theDecoratorclass, and all concrete components are subclassed directly
from theComponentclass. Further on, the concrete components will be wrapped in
concrete decorators, and such distinctions become important in more complex
implementations of the Decorator design pattern. Thetrace( )statement is used to
show you where in the process the abstract Decorator class appears. Save
Example 4-2 asDecorator.as.
Once the two abstract classes,ComponentandDecorator, have been established, it’s
time to work with the concrete classes. For this example, only a single concrete com-
ponent is created. Cleverly namedConcreteComponent, this class represents whatever
will be decorated in a Decorator design pattern. You can have multiple concrete
components that all use the same set of decorations, or only a single one. Later in
this chapter, you will see an application where multiple concrete classes are deco-
rated by a single set of decorators. The nice thing about the Decorator is that you can
add as many concrete components as you want. Imagine a business web site where
the concrete component represents an e-business site that you’ve worked on for sev-
Example 4-2. Decorator.as
package
{
//Abstract Decorator in Decorator Design Pattern
//**
//Abstract class
public class Decorator extends Component
{
trace("||Decorator||");
override public function getInformation( ):String
{
return information;
}
}
}