412 | Chapter 11: Strategy Pattern
Adding a new trick
Just as adding a new concrete context class in a Strategy pattern is easy, so too is
adding a new concrete strategy. Save Example 11-19 using the caption name as the
filename. The new script is indistinguishable from any of the other concrete strate-
gies except for the name of the class and the content in thetrick method.
Revising clown college
To see both how easy it is to add new elements to the application and to dynami-
cally add new strategies to a concrete context class, theClownCollegeclass is revised
(bold text). First, the new concrete clown class,Bubbles, is instantiated and the two
existing delegation methods launch the operations to display the skit and tricks.
Then, the clown instance uses the newsetTrick( )method to add the new trick to its
repertoire. Finally, thedoTrick( )method is launched a second time to display the
new trick. Save Example 11-20 using the caption name for the filename.
Example 11-18. Bubbles.as
package
{
class Bubbles extends Clown
{
trace(" =>Bubbles<= ");
public function Bubbles( )
{
tricks=new Juggle( );
skits = new FallDown( );
}
}
}
Example 11-19. BubblePants.as
package
{
class BubblePants implements Tricks
{
public function trick( ):void
{
trace("Woo woo woo! Bubbles are coming out of my pants!\n");
}
}
}
Example 11-20. ClownCollege.as
package
{
import flash.display.Sprite;
public class ClownCollege extends Sprite