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.

Employing Flexibility in the Template Method<br />

Now that you have a better idea of how the Template Method design pattern’s structure<br />

works, let’s see how it can be usefully employed. In this next example, you’ll see<br />

an implementation that uses the same template method for two different concrete<br />

classes. Figure 9-4 shows the class diagram:<br />

WoodShed<br />

do<strong>Design</strong>()<br />

determineSupplies()<br />

ShedMaker<br />

templateMethod()<br />

do<strong>Design</strong>()<br />

determineSupplies()<br />

storeTools()<br />

do<strong>Design</strong>()<br />

determineSupplies()<br />

storeTools()<br />

Figure 9-4. Template method with two concrete classes<br />

SteelShed<br />

do<strong>Design</strong>()<br />

determineSupplies()<br />

The main abstract class, ShedMaker, includes three operations in the templateMethod( )<br />

function. Two are abstract and will be overridden by the subclasses where the details<br />

will be supplied. The third operation is a final function, not to be overridden, and will<br />

be used in all subclasses.<br />

The example looks at what you may need in creating a shed in general, and what<br />

different types of sheds may need. The example develops an algorithm that does the<br />

following:<br />

• <strong>Design</strong>s the shed<br />

• Determines which supplies will be required<br />

• Stores tools in the <strong>com</strong>pleted shed<br />

All sheds can use these steps, but depending on the kind of shed, the details will vary<br />

in certain aspects. In this case, we’re supposing that one shed will be made of wood<br />

and the other of corrugated steel. Because designing with wood is different than<br />

designing for steel, the design operation (do<strong>Design</strong>( )) is abstract. Likewise, determining<br />

which supplies you need is different as well, and so the determineSupplies( )<br />

function is also abstract. However, no matter what kind of shed is built, storing tools<br />

is the same, and so that function is locked so it won’t be overridden.<br />

To get started, open three new <strong>ActionScript</strong> files, and enter the code in Examples 9-4<br />

to 9-6. Use the caption names as the filenames, and save all files in the same directory.<br />

Employing Flexibility in the Template Method | 341

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

Saved successfully!

Ooh no, something went wrong!