ActionScript 3.0 Design Patterns

(Chris Devlin) #1

404 | Chapter 11: Strategy Pattern


More Delegation in a Concrete Context


In a concrete context class as in Example 11-4, you can see that the work is dele-


gated to a concrete strategy class.


Pulling All the Parts Together


Finally, to test the Strategy design pattern, Example 11-5’s script creates an instance


of the concrete context, but types the instance as aContextdata type—an example of


programming to the interface and not the implementation.


Example 11-3. ConcreteStrategy.as


package
{
class ConcreteStrategy implements Strategy
{
public function think( ):void
{
trace("Great thoughts now...");
}
}
}


Example 11-4. ConcreteContext.as


package
{
class ConcreteContext extends Context
{
public function ConcreteContext( )
{
strategy = new ConcreteStrategy( );
}
}
}


Example 11-5. TestStrategy.as


package
{
import flash.display.Sprite;


public class TestStrategy extends Sprite
{
public function TestStrategy( )
{
var thinker:Context= new ConcreteContext( );
thinker.doStrategy( );
}
}
}

Free download pdf