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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Both instances employ the templateMethod( ) function, but as you will see when you<br />

test the application, the results are very different. Open a new Flash document file,<br />

and in the Document class window, type in BuildShed. When you test the application,<br />

you’ll see the following in the Output window:<br />

SteelShed<br />

<strong>Design</strong>ing Steel Shed<br />

Ok I'll need some corregated sheet metal.<br />

Better get some steel fasteners too.<br />

Now I can put all my tools away.<br />

WoodShed<br />

<strong>Design</strong>ing Wood Shed<br />

I'll do it with 1 X 12's for the walls.<br />

The rest I'll do with 2 X 4's.<br />

Now I can put all my tools away.<br />

In this output, you can see the Template Method at work. In the first instance, you<br />

can see that it shows the SteelShed concrete class has been invoked. All the output is<br />

uniquely relevant to the steel shed except for the last line before the wood shed output.<br />

That last line is the locked up operation from the abstract class, ShedMaker.<br />

Then, the WoodShed instance gets an entirely different set of results when it invokes<br />

the templateMethod( ), with the exception of the last line.<br />

From this example, we hope you can better see the conceptual structure of the<br />

Template Method. All the calls are made from the higher-level ShedMaker class to the<br />

lower-level concrete class instances through the templateMethod( ) function. So even<br />

though the lower level override functions define the specifics of some of the<br />

operations within the template method, the actual call follows the Hollywood Principle<br />

and calls downward through the template method.<br />

Selecting and Playing Sound and Video<br />

Considering how Flash handles sound and video, they both share some actions in<br />

<strong>com</strong>mon. For example, both need to have filenames to play, and both have to somehow<br />

have a “play” <strong>com</strong>mand. Using the Template Method, we should be able to key<br />

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

algorithm. However, because of the differences between the two media, we’ll leave<br />

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

abstract class will be a text header.<br />

Setting Up the Format<br />

The initial abstract class containing an algorithm for a filename selector and play<strong>com</strong>mand<br />

operation is pretty simple, because the operations have no content. A<br />

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

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

344 | Chapter 9: Template Method Pattern

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

Saved successfully!

Ooh no, something went wrong!