O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Minimalist Abstract State Pattern

This first example cuts to the bare bones of a Strategy design pattern to reveal the structure of the design pattern. Each part is analyzed in terms of what it does for the overall pattern. This example should help make the structure clearer.

You need to see the Strategy design pattern in a context where several different objects delegate behaviors to different delegates. (The second example provides this richer applicability.) In this minimalist example, you need to look at the points of delegation and how it all works. In Figure 11-3, following the listing, you will be able to see what looks like a web of intrigue. Not to worry; the illustration just shows the delegation work going on between the objects in the application.

Using Delegation with the Context

The Context class 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 reference 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.)

Example 11-1. Context.as

package
{
    class Context
    {
        protected var strategy:Strategy;

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

The Context class is pretty simple. One reason is that all the methods it needs are delegates implemented from the Strategy interface. Let's now look at such an interface.

Adding a Strategy

In a Strategy design pattern, you're ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required