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.

method. The child classes just give the details to the operations within an algorithm<br />

controlled by the parent class.<br />

The template method itself is locked using the final attribute. By adding final to a<br />

function, the function cannot be overridden. At first, this may seem to contradict<br />

everything being said about leaving some of the details to the subclasses by overriding<br />

the operations. However, you don’t lock the abstract functions (operations), only<br />

the template method. Figure 9-1 illustrates this arrangement.<br />

class AbstractClass<br />

{<br />

final function templateMethod()<br />

{<br />

operationA()<br />

operationB()<br />

operationC()<br />

}<br />

Figure 9-1. Locked Template Method<br />

As you can see in Figure 9-2, the operations that make up the template method are<br />

not locked. So the subclasses can override the operations all they need. However,<br />

subclasses cannot change the order of the operations that make up the algorithm in<br />

the template method itself.<br />

Finally, you can add an optional hook operation. On one level, a hook is nothing<br />

more than a method that can be overridden in the context of a Template Method<br />

design. It also can be seen as a back door where an implementation (subclass) can<br />

hook into an algorithm. Because the hook concept is best understood in the context<br />

of an example, further discussion will be deferred to later on in the chapter.<br />

The Template Method Model<br />

Upon first encountering the Template Method, most developers are struck by the<br />

simplicity of the pattern’s design, and then by its utility and clarity. Even though the<br />

design as depicted in the class diagram in Figure 9-2 is very simple, it can be used to<br />

solve a wide range of problems.<br />

334 | Chapter 9: Template Method Pattern<br />

}<br />

function operationA()<br />

{}<br />

function operationB()<br />

{}<br />

function operationC()<br />

{}<br />

Locked up<br />

Unlocked

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

Saved successfully!

Ooh no, something went wrong!