ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Minimalist Abstract State Pattern | 403

Using Delegation with the Context


TheContextclass shown in Example 11-1 is an aggregator—it owns the Strategy


object. So in every Context class where Strategy patterns are found, you will find a ref-


erence to the strategy class—in this example a variable taking the lowercase name of


the Strategy interface. (Example 11-1 to Example 11-5 make up the first application.)


TheContextclass is pretty simple. One reason is that all the methods it needs are del-


egates implemented from theStrategy interface. Let’s now look at such an interface.


Adding a Strategy


In a Strategy design pattern, you’re likely to see more than one interface, depending


on the different types of methods required. Example 11-2 shows a single method in


the interface.


This represents the first step in encapsulating a behavior. The subsequent concrete


Strategy implementations add detail.


Details of the Strategy


Next, in Example 11-3, a concrete strategy adds a method,think( ), which does


something. In this case, it just sends out a trace message.


Example 11-1. Context.as


package
{
class Context
{
protected var strategy:Strategy;


public function doStrategy( ):void
{
strategy.think( );
}
}
}


Example 11-2. Strategy.as


package
{
interface Strategy
{
function think( ):void;
}
}

Free download pdf