152 | Chapter 4: Decorator Pattern
If you break down Example 4-21, the first part looks exactly like the previous exam-
ples. AComponentinstance,components, is instantiated, and the constructor function
wraps thecomponents object in itself:
var components:Component;
public function GoodEvil(components:Component)
{
this.components=components;
}
Next, the script re-implements the getter function,getSoul( ), to return both the cur-
rent value of the concrete component’s decorations plus its own decoration value.
Again, this is what previous examples have done.
override public function getSoul( ):String
{
return components.getSoul( ) + "|GoodEvil";
}
The next two functions add or subtract numeric values using thegood( )andevil( )
methods. Each good adds to a good and subtracts from an evil, andvice versafor a
vice—adds to evil and subtracts from good. So depending on the concrete decora-
tor, you add or subtract from each of the two return values, and add that to the cur-
rent value of the concrete component.
override public function good( ):Number
{
return +/-00 + components.good( );
Example 4-21. Generic concrete decoration
package
{
//Generic—NOT implemented
public class GoodEvil extends Decorator
{
private var components:Component;
public function GoodEvil(components:Component)
{
this.components=components;
}
override public function getSoul( ):String
{
return components.getSoul( ) + "|GoodEvil";
}
override public function good( ):Number
{
return +/-00 + components.good( );
}
override public function evil( ):Number
{
return +/-00 + components.evil( );
}
}
}