ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Adding More Concrete Strategies and Concrete Contexts | 411

The output represents the algorithms set up in the strategy classes. All are delegated


from theClown context class and implemented in the concrete clown classes.


Additional Clown Functionality


As noted in the initial clown application using the Strategy design pattern, the struc-


ture allowed very little flexibility and no way to dynamically change a concrete con-


text to accept another strategy. So what happens if we add another clown who can


do more than one trick but not all the tricks?


Fortunately, we have a simple solution. By adding setters to the context class,Clown,


it will be possible to dynamically add strategies to the concrete context classes—the


clowns. Example 11-17 revises the originalClownclass by adding the setters (shown


in bold).


Adding a new clown


The application is already structured to easily accept a new concrete context class. So


all we have to do is to add a subclass, and include the concrete strategies to be dele-


gated. Example 11-18 does just that. Save the file using the caption as the filename.


Example 11-17. Clown.as


package
{
class Clown
{
protected var tricks:Tricks;
protected var skits:Skits;


public function doTrick( ):void
{
tricks.trick( );
}


public function doSkit( ):void
{
skits.skit( );
}


public function setTrick(addTrick:Tricks):void
{
tricks=addTrick;
}


public function setSkit(addSkit:Tricks):void
{
tricks=addSkit;
}
}
}

Free download pdf