ActionScript 3.0 Design Patterns

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

If you want to create more concrete component classes, all you need to do is create a


similar class with a different name and value for thewhatToWearvariable. With this


structure, you have no limit to the number of new concrete components you can add.


Concrete decorator classes


Moving from the minimalist example previously shown in this chapter, it’s a little


easier to see how the Decorator pattern works by actually seeing something happen-


ing in a graphic display. When the initial instance of the concrete component is cre-


ated, all references in the concrete decorator class are to that instance. (See example


4-16, where the concrete component is wrapped in the decorators.) In all the con-


crete decorator classes, the reference to themodelvariable is a reference to the con-


crete component object. In this case, that’s the instance of theSue( )class, but it can


be any instance of any concrete component. That’s why, if you wish to expand the


application to include more concrete components (paper dolls to dress), you don’t


have to make any fundamental changes. Just add another concrete component class.


In Examples 4-10 to 4-15, the captions are the filenames.


Example 4-10. OrangeDress.as


package
{
public class OrangeDress extends Dresser
{
private var model:Model;
public function OrangeDress(model:Model)
{
this.model=model;
}
override public function getDressed( ):String
{
return model.getDressed( ) + "~orangedress";
}
}
}


Example 4-11. BlueDress.as


package
{
public class BlueDress extends Dresser
{
private var model:Model;
public function BlueDress(model:Model)
{
this.model=model;
}
override public function getDressed( ):String
{
return model.getDressed( ) + "~bluedress";
}
}
}

Free download pdf