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.

Open a new Flash document, and type TestFlight into the document class window.<br />

You should see the following results:<br />

*^* Clear as a Bell *^*<br />

Doing the walk around...looking good.<br />

I'm off to Baja on a beautiful day!<br />

First I'll file a VFR flightplan<br />

*^* Cloudy *^*<br />

Doing the walk around...looking good<br />

In the first part, you can see that both of the algorithm’s operations successfully output<br />

the checkAirplane( ) and fileVFR( ) method results. However, in the Cloudy<br />

instance, only the checkAirplane( ) method invokes. This is as expected because in<br />

the Cloudy class, the checkWeather( ) hook method returned something other than<br />

“Nice and clear,” thereby canceling the fileVFR( ) operation.<br />

Summary<br />

The Template Method design pattern shows how to distribute behavior among subclasses.<br />

It leaves much of the detail work to the subclasses, but at the same time it<br />

can control the order of the algorithm making up the template method. It stands as a<br />

good example of controlling dependencies and use of the inverted control structure.<br />

As such, the Template Method has many uses where both control and flexibility are<br />

required while minimizing dependency. It is a widely used pattern, and in many<br />

abstract classes you can find some a template method at work to some degree. It’s<br />

truly a general pattern because it offers both algorithm control and subclass flexibility<br />

in implementing the class methods.<br />

The Template Method in <strong>ActionScript</strong> <strong>3.0</strong> is quite useful even though the language<br />

lacks abstract functions and classes. By treating the default internal functions with<br />

no content as though they are abstract operations, it’s possible to use template methods<br />

that are virtually identical to languages that feature both abstract classes and<br />

functions.<br />

Finally, hook methods offer further flexibility without really disrupting the main<br />

algorithm’s flow. These methods can act like “back doors” to the main algorithm. All<br />

the operations in the template methods stay in the same order, but they can be<br />

changed in terms of what they do, or if they’re omitted in the invocation of the template<br />

method. It’s little wonder that the Template Method, with its strong structure<br />

and flexible implementation, is so widely used.<br />

356 | Chapter 9: Template Method Pattern

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

Saved successfully!

Ooh no, something went wrong!