Using Concrete Decorator Classes - Implementing the Good and Evil Decorator
(Page 3 of 4 )
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 the Dick andJaneconcrete 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 in MainDual in the Document class window in the Dual.fla Properties panel, and resave it as MainDual.as. Now test the movie.
Example 4-36. MainDual.as
package
{
import flash.display.Sprite;
public class MainDual extends Sprite
{
public function MainDual()
{
trace("||--Judging--||");
var life:Component=new Dick();
var light:Component=new Jane();
//***Add Good
life=new Courage(life);
light=new Hope(light);
light=new Compassion(light);
life=new Openness(life);
life=new Diligence(life);
light=new Justice(light);
//***Add Evil
light=new Malice(light);
life=new Prejudice(life);
life=new Dogmatisms(life);
life=new Arrogance(life);
life=new Indifference(life);
life=new Rage(life);
light=new Obsfuscation(light);
trace(life.getSoul()+"\nTotal Virture: "+
(life.good()-life.evil()));
trace(light.getSoul()+"\nTotal Virture: "+
(light.good()-light.evil()));
}
}
}
The output for all of the good and evil together is:
||--Judging--||
Dick's soul
|Courage|Openness|Diligence|Prejudice| Dogmatisms|Arrogance
|Indifference|Rage
Total Virture: -38
Jane's soul
|Hope|Compassion|Justice|Malice| Obsfuscation
Total Virture: 10
Because thetrace()statement subtracts theevil()method from thegood()method total value, any positive results indicate an abundance of good characteristics, and a negative result shows a plethora of negative traits.
You can “multi-wrap” using the same decorator more than once on the same concrete component. (Someone who thinks he can get the month-behind project done on time by not sleeping for a week is doubly hopeful, has a triple dose of diligence, and perhaps a double dose of arrogance.)
Next: Charting souls >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of ActionScript 3.0 Design Patterns Object Oriented Programming Techniques, written by William B. Sanders and Chandima Cumaranatunge (O'Reilly, 2007 ISBN: 0596528469). Check it out today at your favorite bookstore. Buy this book now.
|
|