406 | Chapter 11: Strategy Pattern
Adding More Concrete Strategies and Concrete Contexts
The purpose of the minimalist example is to expose a design pattern’s structure.
However, with the Strategy pattern, a more robust example may actually do a better
job of showing the pattern’s features, because it places the pattern in a more practi-
cal context in the bare bones example.
In this next example, we envision a Strategy pattern used with a clown employment
agency. The agency has a number of clowns it represents. Clients call for different
clown venues—circuses, birthday parties, political conventions. They want either a
particular clown or particular clown acts (skits) or tricks they can perform. To create
a flexible program for the clown agency, each of the skits and tricks has been created
in separate algorithms. The algorithms are encapsulated, and the clown characters
delegate their performance skills to the encapsulated algorithms—strategies.
The Clowns
The first step is to create a context class and concrete contexts that will use the dif-
ferent tricks and skits. Example 11-6 is the main context class establishing the refer-
ences to the strategy methods. The trick and skit operations are delegated to the
strategy classes. Then Examples 11-7 and 11-8 create two concrete clowns. Each is
assigned a different trick and skit that are delegated to concrete strategy instances.
To get started, open three ActionScript files and enter the code in Examples 11-6 to
11-8, saving each with the caption name provided. (Examples 11-6 through 11-16
make up the entire application.)
Example 11-6. Clown.as
package
{
class Clown
{
protected var tricks:Tricks;
protected var skits:Skits;
public function doTrick( ):void
{
tricks.trick( );
}
public function doSkit( ):void
{