10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Delegation with the Context<br />

The Context class shown in Example 11-1 is an aggregator—it owns the Strategy<br />

object. So in every Context class where Strategy patterns are found, you will find a reference<br />

to the strategy class—in this example a variable taking the lowercase name of<br />

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

Example 11-1. Context.as<br />

package<br />

{<br />

class Context<br />

{<br />

protected var strategy:Strategy;<br />

}<br />

}<br />

public function doStrategy( ):void<br />

{<br />

strategy.think( );<br />

}<br />

The Context class is pretty simple. One reason is that all the methods it needs are delegates<br />

implemented from the Strategy interface. Let’s now look at such an interface.<br />

Adding a Strategy<br />

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

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

the interface.<br />

Example 11-2. Strategy.as<br />

package<br />

{<br />

interface Strategy<br />

{<br />

function think( ):void;<br />

}<br />

}<br />

This represents the first step in encapsulating a behavior. The subsequent concrete<br />

Strategy implementations add detail.<br />

Details of the Strategy<br />

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

something. In this case, it just sends out a trace message.<br />

Minimalist Abstract State Pattern | 403

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!