ActionScript 3.0 Design Patterns

(Chris Devlin) #1

344 | Chapter 9: Template Method Pattern


Both instances employ thetemplateMethod( )function, but as you will see when you


test the application, the results are very different. Open a new Flash document file,


and in the Document class window, type inBuildShed. When you test the applica-


tion, you’ll see the following in the Output window:


SteelShed
Designing Steel Shed
Ok I'll need some corregated sheet metal.
Better get some steel fasteners too.
Now I can put all my tools away.

WoodShed
Designing Wood Shed
I'll do it with 1 X 12's for the walls.
The rest I'll do with 2 X 4's.
Now I can put all my tools away.

In this output, you can see the Template Method at work. In the first instance, you


can see that it shows theSteelShedconcrete class has been invoked. All the output is


uniquely relevant to the steel shed except for the last line before the wood shed out-


put. That last line is the locked up operation from the abstract class,ShedMaker.


Then, theWoodShedinstance gets an entirely different set of results when it invokes


thetemplateMethod( ), with the exception of the last line.


From this example, we hope you can better see the conceptual structure of the


Template Method. All the calls are made from the higher-levelShedMakerclass to the


lower-level concrete class instances through thetemplateMethod( )function. So even


though the lower level override functions define the specifics of some of the


operations within the template method, the actual call follows the Hollywood Princi-


ple and calls downward through the template method.


Selecting and Playing Sound and Video


Considering how Flash handles sound and video, they both share some actions in


common. For example, both need to have filenames to play, and both have to some-


how have a “play” command. Using the Template Method, we should be able to key


in on those two functions of getting a filename and playing, and placing them into an


algorithm. However, because of the differences between the two media, we’ll leave


the details up to the concrete classes. The locked operation that’ll be in the main


abstract class will be a text header.


Setting Up the Format


The initial abstract class containing an algorithm for a filename selector and play-


command operation is pretty simple, because the operations have no content. A


third operation that adds a text message to the top of the screen will be locked, and


while it’s relatively simple, it requires importing a number of packages. Open a new

Free download pdf