ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Applying a Simple Decorator Pattern in Flash: Paper Doll | 141

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 deco-


rations are clothing items selected for the doll. Using ActionScript’s newLoaderclass,


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 applica-


tion’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 sub-


classes. 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.


Keep in mind that Example 4-7 should be treated as an abstract class. So the method


getDressed( )needs to be created as an override public function for it to work the


way we want. However, the propertywhatToWeardoesn’t need to be changed from its


inherited characteristics.


Decorator Class: Dressing the Dolls


Example 4-8 is the abstract Decorator class,Dresser,which extends the component


class,Model. The major contribution here is simply re-implementing thegetDressed( )


method with a reference to the inheritedwhatToWear property.


Example 4-7. Model.as


package
{
//Abstract class
public class Model
{
protected var whatToWear:String;
public function getDressed( ):String
{
return whatToWear;
}
}
}

Free download pdf