ActionScript 3.0 Design Patterns

(Chris Devlin) #1

332 | Chapter 9: Template Method Pattern


to inspect. So, you’re going to need special instructions for the first item in the out-


line depending on the vehicle.


This chapter contains two different types of references to “template
method.” A reference to the Template Method design pattern will
have the first letter of each word capitalized. The template method is
also a reference to a method (function), and that reference will use
lowercase for both words.

For the second step, navigation in a car requires getting on the road, making all the


correct turns, and using the right streets. This can be tricky at times, and the driver


may want to check on traffic reports to avoid congestion or road repairs. Depending


on the circumstances, a boat trip can be anything from a quick trip across a lake to a


comple xnavigation through shoals, shallows, and currents. Finally, navigating an


airplane can be a simple straight flight on a clear day to an IFR (Instrument Flight


Rules) flight requiring knowledge of several different navigation instruments in


cloudy weather. Like the first step, this too will require specialized instructions.


Finally, you’ve got the cup of coffee you drink on the way to work. That’s pretty


much the same no matter what kind of transportation you use. You’d use the same


kind of travel container with all three modes of transportation, and you’d have to


take care not to spill coffee in your lap. Unlike the first two sets of instructions that


differ for each mode of transportation, this last one’s going to be the same for all


three vehicles.


Making a template for this set of circumstance requires a loosely crafted method for


the first two items and a concrete one for the last. Using the Template Method


design, each of the three functions would be placed in a method so that the sequence


of the steps would be preserved no matter what. The method containing the ordered


functionsis the template method—a method to preserve the sequence. Here’s what it


would look like in a rough pseudocode:


//Within an abstract class

//This the the Template Method
final function templateMethod( )
{
prepareTransportation( );
navigatVehicle( );
drinkCoffee( );
}

//Abstract functions
function prepareTransportation( ):void
{
//nothing here
}
Free download pdf