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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example 11-3. ConcreteStrategy.as<br />

package<br />

{<br />

class ConcreteStrategy implements Strategy<br />

{<br />

public function think( ):void<br />

{<br />

trace("Great thoughts now...");<br />

}<br />

}<br />

}<br />

More Delegation in a Concrete Context<br />

In a concrete context class as in Example 11-4, you can see that the work is delegated<br />

to a concrete strategy class.<br />

Example 11-4. ConcreteContext.as<br />

package<br />

{<br />

class ConcreteContext extends Context<br />

{<br />

public function ConcreteContext( )<br />

{<br />

strategy = new ConcreteStrategy( );<br />

}<br />

}<br />

}<br />

Pulling All the Parts Together<br />

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

of the concrete context, but types the instance as a Context data type—an example of<br />

programming to the interface and not the implementation.<br />

Example 11-5. TestStrategy.as<br />

package<br />

{<br />

import flash.display.Sprite;<br />

}<br />

public class TestStrategy extends Sprite<br />

{<br />

public function TestStrategy( )<br />

{<br />

var thinker:Context= new ConcreteContext( );<br />

thinker.doStrategy( );<br />

}<br />

}<br />

404 | Chapter 11: Strategy Pattern

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

Saved successfully!

Ooh no, something went wrong!