336 | Chapter 9: Template Method Pattern
Further, discussing favoring composition over inheritance, GoF note that reuse by
inheritance eases the process of making new components that can be composed with
old ones. Hence the dictum that composition should be favored over inheritance
needs to be tempered by the knowledge that inheritance and composition actually
work together. This is not to say that the principle of favoring composition over
inheritance is invalid, but rather you need to understand the principle in the context
of its development.
In the process of learning design patterns and the programming princi-
ples surrounding them, do not learn the principles as mantras. Chant-
ing the principles over and over again may help you remember them,
but not necessarily understand them. All the principles exist in some
context, so be certain to understand the principles’ context to use
them effectively.
Abstract Functions and Override Flexibility
In previous chapters we’ve groused about the fact that ActionScript 3.0 doesn’t have
abstract classes. Well, ActionScript 3.0 doesn’t haveabstract functionseither. Rather
than rant about that fact, a more important issue is their use. An abstract function is
a way of reducing a function to an idea or concept without content. In actual usage,
all this involves is naming the function and placing it in the order you want for your
main algorithm in the template method, not unlike the functions in an interface. In
languages like Java, you can write:
abstract myAbstractFunction( );
and that’s it. In ActionScript 3.0, you can accomplish the same thing using:
function myAbstractFunction( ) {}
So while you don’t have a function that can be designated abstract, you can effec-
tively create all the abstract functions you want, just as you can develop abstract
classes, with ActionScript 3.0.
Aside from the fact that ActionScript 3.0 can create functions that act like abstract
functions, the more important principle is to understand their use in the context of
the Template Method design pattern. Key to all design patterns is the idea of reus-
ability and flexibility. Abstract functionsmust beoverridden, and therein is their flex-
ibility. Whenever a function is overridden, it’s changed. By setting up abstract
functions in the parent class, the subclasses can use them as needed to fill out the
particular implementation of the template method algorithm. Reuse is fostered by
the fact that as long as the algorithm template is applicable, the developer has flexi-
bility because the abstract functions that make up the template method can be modi-
fied to suit the application.