142 | Chapter 4: Decorator Pattern
All of the concrete decorators will be subclassed for this class.
The Concrete Classes
Once the two main abstract classes in the Decorator pattern have been established,
you’re all set to create the concrete ones. In the previous minimalist Decorator exam-
ple, you saw the output using thetrace( )statement. Instead oftrace( )statements,
both the concrete component and decorators need to be formatted for later parsing,
so a tilde character (~) has been added as a demarcation point. Because all the strings
from both the concrete component and decorators are grouped together into a single
large string, the tilde serves as cutting point.
Concrete component class
The concrete component class is the only concrete class that extends directly from
the abstract concrete class. All the other classes in this application extend from the
abstract decorator class. Using a simple constructor function,Sue( ), the class assigns
a value to thewhatToWearvariable. This is enough to identify the class as an instance
of the main abstract component class,Model, and to establish a unique name. All
decorations use the concrete component as the target for the decorations. Save
Example 4-9 asSue.as.
Example 4-8. Dresser.as
package
{
//Abstract class
public class Dresser extends Model
{
override public function getDressed( ):String
{
return whatToWear;
}
}
}
Example 4-9. Sue.as
package
{
public class Sue extends Model
{
public function Sue( )
{
whatToWear="~sue";
}
}
}