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.

When to Hook?<br />

The hook’s useful when only part of your algorithm needs to have detailed adjustments<br />

made some of the time by the subclasses. Because the subclasses are expected<br />

to provide detail to a template method’s algorithm, you don’t need the hook as often<br />

as you might expect. A hook is a concrete method, but it can still be overridden just<br />

like any abstract method; so it’s not a matter of being written in concrete. Given that<br />

<strong>ActionScript</strong> <strong>3.0</strong> doesn’t differentiate between abstract and concrete functions, the<br />

difference is even more blurred.<br />

One way to approach the use of a hook in a template method is to consider exceptions<br />

in an algorithm. For example, if 9 out of 10 times your subclasses will not override<br />

the hook operation, it’s probably a good idea to use the hook. The less a hook<br />

will be overridden, the more useful it is. This may seem like a contradiction, but consider<br />

that hooks are put into the algorithm because they don’t have to be overridden.<br />

Most of the time, the subclasses can just implement the template method and<br />

be done with it, using the default value provided by your hook. However, when the<br />

subclass needs to use the hook to provide an exception to a default value, then it<br />

be<strong>com</strong>es useful.<br />

The Hook as a Lonely Repairman<br />

For years the Maytag Company depicted their repairmen as idle and lonely. The message<br />

was that Maytag products were so well manufactured that the repairmen’s services<br />

were unnecessary. We can look at the hook in the template method in the same<br />

light—lonely repairmen who are seldom required.<br />

The hook’s use is an exception; so we need to consider a scenario where conditions<br />

are usually the same but occasionally change, where we need the hook. One of the<br />

great flying areas can be found along the desert border area in Southern California.<br />

The weather is typically clear with almost unlimited visibility, allowing pilots to fly<br />

under Visual Flight Rules (VFR). Flying down to Baja, Mexico is a beautiful trip,<br />

especially along the Sea of Cortez on the eastern side of the Baja peninsula. Consider<br />

a pilot who flies tourists down to Punta Bufeo in Baja to visit the sea lion rookery.<br />

With just a few exceptions, the pilot can file a VFR flight plan and take off every day.<br />

However, every now and then, the weather conditions are less than VFR conditions—cloudy,<br />

foggy or extremely hazy. Because the pilot is rated only for VFR conditions,<br />

he can’t fly under non-VFR or Instrument Flight Conditions (IFR). Using the<br />

hook, we’ll create a template method to deal with these the situation.<br />

Baja Flight with a Hook<br />

Using our hypothetical Baja pilot, this next application shows how to employ a hook<br />

operation in a Template Method design pattern. We’re assuming that the pilot flies<br />

out of the Calexico airport where the weather’s usually sunny and clear. Open four<br />

352 | Chapter 9: Template Method Pattern

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

Saved successfully!

Ooh no, something went wrong!