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

Hooking It Up

Controlling subclass extensions is one use for the Template Method. In discussing applicability, the originators of the design pattern concepts (GoF) include what they call a "hook" operation. The hook operation is placed into the template method to allow extensions to the hook in the main algorithm.

For ActionScript 3.0 programmers, hook functions look exactly like the abstract functions. That's because ActionScript 3.0 has no real abstract functions, so we have to keep in mind that the difference between hook operations and abstract ones is that hook operations can be overridden, while abstraction operations must be overridden. This is where comment lines come in handy, as the following script shows:

//Abstract function
function doAbstract():void {}

//Hook function
function doHook():void {}

Neither the abstract function nor the hook function has to have any content. However, each is set up differently, and adding content to the hook operation provides a way of setting up default conditions.

When to Hook?

The hook's useful when only part of your algorithm needs to have detailed adjustments made some of the time by the subclasses. Because the subclasses are expected to provide detail to a template method's algorithm, you don't need the hook as often as you might expect. A hook is a concrete method, but it can still be overridden just like any abstract method; so it's not a matter of being written in concrete. Given that ActionScript 3.0 doesn't differentiate between abstract ...

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