Applying a Simple Decorator Pattern in Flash: Paper Doll
You can move from a conceptual example to a concrete one with a computerized paper doll game. The paper doll component is the doll being dressed, and the decorations are clothing items selected for the doll. Using ActionScript’s new Loader class, different GIF files are used to decorate a base GIF image of a Victorian paper doll. As in the previous abstract example, this example uses a single concrete component and several decorations.
This concrete example does not have an interface—keeping the focus on the application’s use of a Decorator pattern. However, it does have a graphic output so you can see the pattern’s ultimate output. (Later in this chapter, there’s an example with a full interface.)
Setting Up the Component Class
The first class is the component class, Model. From this, all other classes are subclasses. It’s very simple but key to the success of the application. As you can see, it’s very close to the previous abstract Decorator. It consists of a method, getDressed(), and a string variable,whatToWear,as shown in Example 4-7. Save the file asModel.as.
Example 4-7. Model.as
package { //Abstract class public class Model { protected var whatToWear:String; public function getDressed():String { return whatToWear; } } }
Keep in mind that Example 4-7 should be treated as an abstract class. So the methodgetDressed()needs to be created as an override public function for it to work the way we want. However, the propertywhatToWear doesn’t need to be changed from its inherited characteristics.