352 | Chapter 9: Template Method Pattern
When to Hook?
The hook’s useful when only part of your algorithm needs to have detailed adjust-
ments madesome of the timeby 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 and concrete functions, the
difference is even more blurred.
One way to approach the use of a hook in a template method is to consider excep-
tions in an algorithm. For example, if 9 out of 10 times your subclasses will not over-
ride the hook operation, it’s probably a good idea to use the hook.The less a hook
will be overridden, the more useful it is. This may seem like a contradiction, but con-
sider that hooks are put into the algorithm because they don’t have to be overrid-
den. Most of the time, the subclasses can just implement the template method and
be done with it, using the default value provided by your hook. However, when the
subclass needs to use the hook to provide an exception to a default value, then it
becomes useful.
The Hook as a Lonely Repairman
For years the Maytag Company depicted their repairmen as idle and lonely. The mes-
sage was that Maytag products were so well manufactured that the repairmen’s ser-
vices were unnecessary. We can look at the hook in the template method in the same
light—lonely repairmen who are seldom required.
The hook’s use is an exception; so we need to consider a scenario where conditions
are usually the same but occasionally change, where we need the hook. One of the
great flying areas can be found along the desert border area in Southern California.
The weather is typically clear with almost unlimited visibility, allowing pilots to fly
under Visual Flight Rules (VFR). Flying down to Baja, Mexico is a beautiful trip,
especially along the Sea of Cortez on the eastern side of the Baja peninsula. Consider
a pilot who flies tourists down to Punta Bufeo in Baja to visit the sea lion rookery.
With just a few exceptions, the pilot can file a VFR flight plan and take off every day.
However, every now and then, the weather conditions are less than VFR condi-
tions—cloudy, foggy or extremely hazy. Because the pilot is rated only for VFR con-
ditions, he can’t fly under non-VFR or Instrument Flight Conditions (IFR). Using the
hook, we’ll create a template method to deal with these the situation.
Baja Flight with a Hook
Using our hypothetical Baja pilot, this next application shows how to employ a hook
operation in a Template Method design pattern. We’re assuming that the pilot flies
out of the Calexico airport where the weather’s usually sunny and clear. Open four