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.

Adding More Concrete Strategies and Concrete Contexts<br />

The purpose of the minimalist example is to expose a design pattern’s structure.<br />

However, with the Strategy pattern, a more robust example may actually do a better<br />

job of showing the pattern’s features, because it places the pattern in a more practical<br />

context in the bare bones example.<br />

In this next example, we envision a Strategy pattern used with a clown employment<br />

agency. The agency has a number of clowns it represents. Clients call for different<br />

clown venues—circuses, birthday parties, political conventions. They want either a<br />

particular clown or particular clown acts (skits) or tricks they can perform. To create<br />

a flexible program for the clown agency, each of the skits and tricks has been created<br />

in separate algorithms. The algorithms are encapsulated, and the clown characters<br />

delegate their performance skills to the encapsulated algorithms—strategies.<br />

The Clowns<br />

The first step is to create a context class and concrete contexts that will use the different<br />

tricks and skits. Example 11-6 is the main context class establishing the references<br />

to the strategy methods. The trick and skit operations are delegated to the<br />

strategy classes. Then Examples 11-7 and 11-8 create two concrete clowns. Each is<br />

assigned a different trick and skit that are delegated to concrete strategy instances.<br />

To get started, open three <strong>ActionScript</strong> files and enter the code in Examples 11-6 to<br />

11-8, saving each with the caption name provided. (Examples 11-6 through 11-16<br />

make up the entire application.)<br />

Example 11-6. Clown.as<br />

package<br />

{<br />

class Clown<br />

{<br />

protected var tricks:Tricks;<br />

protected var skits:Skits;<br />

public function doTrick( ):void<br />

{<br />

tricks.trick( );<br />

}<br />

public function doSkit( ):void<br />

{<br />

406 | Chapter 11: Strategy Pattern

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

Saved successfully!

Ooh no, something went wrong!