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.

function navigateVehicle( ): void<br />

{<br />

//nothing here<br />

}<br />

//Concrete function<br />

function drinkCoffee( )<br />

{<br />

trace("Drinking coffee on the way to work ");<br />

}<br />

//End of class<br />

The Template Method does not require a concrete function. However, the example<br />

illustrates that if you have one that’s needed in all instantiations of the template<br />

method, it’s handy to have.<br />

Key Features<br />

In general, the Template Method design pattern is a flexible algorithm-maker centered<br />

around the domain class where the algorithm’s defined. The following features<br />

characterize the Template Method design pattern:<br />

• Uses inheritance for behavior distribution between classes<br />

• Allows subclasses to provide details for some operations in general algorithm<br />

• Uses inverted control structure—parent class calls operations of a subclass<br />

• Specifies the steps of an algorithm and locks the order of operations in the<br />

algorithm<br />

• Allows for optional “hook” operations for extensions at specified points<br />

In the Behavior grouping of design patterns, the GoF specifies only two patterns<br />

based on inheritance instead of <strong>com</strong>position, and the Template Method is one.<br />

Because <strong>ActionScript</strong> <strong>3.0</strong> doesn’t have true abstract classes, you have to remember<br />

not to implement the main abstract class. However, you can program to the abstract<br />

classes interface (supertype), and not the implementation. This chapter’s examples<br />

show this.<br />

By allowing the subclasses to fill in some operations in the different parts of the algorithm,<br />

the design pattern allows for both the advantages of inheritance and the flexibility<br />

of <strong>com</strong>position. Essentially the Template Method uses inheritance to vary parts<br />

of the algorithm. Because the operations allow the subclasses to fill in their details,<br />

think of the operations as placeholders awaiting details from the subclasses. By selectively<br />

overriding the algorithm’s (template method’s) operations, each subclass can<br />

get the functionality it needs.<br />

The inverted control structure is sometimes called the Hollywood Principle because<br />

the parent classes in effect declare, “Don’t call us, we’ll call you,” to the child classes<br />

(subclasses). This makes the general domain as defined in the abstract class the epicenter.<br />

The parent class calls the operations of a subclass through the template<br />

What Is the Template Method Pattern? | 333

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

Saved successfully!

Ooh no, something went wrong!