ActionScript 3.0 Design Patterns

(Chris Devlin) #1

160 | Chapter 4: Decorator Pattern


At this point you can add your own concrete decorators. Use the same format as the


others. You can also add additional concrete components. So instead of just Dick


and Jane, you can add others you’d like to decorate with good and evil.


Implementing the Good and Evil Decorator


Instead of a single implementation, you can try out two different implementations.


The first one dresses up the two different concrete components, and sends the results


to the output window. The second takes the results, uses them to place movie clips


on a “soul graph,” and adds a label to an angel or devil movie clip—depending on


whether good or evil is predominant.


Dual implementation


The first implementation decorates both theDickandJaneconcrete components.


This one is set up to use all 14 deadly sins and heavenly virtues, but you can use any


combination you want. As each concrete component (lifeandlight) is wrapped,


the good and evil properties are incremented or decremented, depending on which


decorator wraps the concrete component. The reference to thecomponentsobject in


each of the decorators is a reference to the concrete component being wrapped. With


each new decorator, then, the value goes up and down depending on the wrapping


decorator. Save Example 4-36 asMainDual.asin an ActionScript file. Open a new


Flash document file, and save it as aDual.fla. Type inMainDualin the Document


Example 4-35. Indifference.as


package
{
public class Indifference extends Decorator
{
private var components:Component;
public function Indifference(components:Component)
{
this.components=components;
}
override public function getSoul( ):String
{
return components.getSoul( ) + "|Indifference";
}
override public function good( ):Number
{
return -9 + components.good( );
}
override public function evil( ):Number
{
return 10 + components.evil( );
}
}
}

Free download pdf