ActionScript 3.0 Design Patterns

(Chris Devlin) #1
331

Chapter 9
CHAPTER 9


Template Method Pattern 9


If you write a post and put it on a blog, that’s a
historical document. If you change your template, then
that entry looks completely different. It’s the same
words, but not the same meaning. This all depends on
what historical questions that people will be asking
and we can’t know what they will want.
—Josh Greenberg
Well, I did all the pre-production and I did full demos
of all the songs and then I took it into the studio and
played it for all the guys and then we kind of took that
as the template and did the album live very quickly.
—David Sanborn

What Is the Template Method Pattern?


You’re going to like this pattern. Essentially thetemplate methodis an algorithm


made up of a sequence of operations that accomplishes some goal. Imagine a general


set of steps for getting something done such as going to work. If you take a car, boat,


or airplane to work, you’re going to be doing certain things that are similar and oth-


ers that are different. You’ll be transporting yourself in a vehicle, guiding it to a par-


ticular location, and you always grab a cup of coffee to take with you. So if you make


a general template that covers going by car, boat, or airplane, your outline (algo-


rithm) would look something like the following:



  1. Prepare transportation.

  2. Navigate vehicle.

  3. Drink your cup of coffee.


Preparing a car for transportation usually involves little more than making sure it’s


got enough gas and the tires aren’t flat. With a boat, you might want to check to


make sure you have the necessary charts on board and that all the life preservers are


in good shape. An airplane requires a walk around with a pre-flight checklist of items

Free download pdf