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.

Further, discussing favoring <strong>com</strong>position over inheritance, GoF note that reuse by<br />

inheritance eases the process of making new <strong>com</strong>ponents that can be <strong>com</strong>posed with<br />

old ones. Hence the dictum that <strong>com</strong>position should be favored over inheritance<br />

needs to be tempered by the knowledge that inheritance and <strong>com</strong>position actually<br />

work together. This is not to say that the principle of favoring <strong>com</strong>position over<br />

inheritance is invalid, but rather you need to understand the principle in the context<br />

of its development.<br />

In the process of learning design patterns and the programming principles<br />

surrounding them, do not learn the principles as mantras. Chanting<br />

the principles over and over again may help you remember them,<br />

but not necessarily understand them. All the principles exist in some<br />

context, so be certain to understand the principles’ context to use<br />

them effectively.<br />

Abstract Functions and Override Flexibility<br />

In previous chapters we’ve groused about the fact that <strong>ActionScript</strong> <strong>3.0</strong> doesn’t have<br />

abstract classes. Well, <strong>ActionScript</strong> <strong>3.0</strong> doesn’t have abstract functions either. Rather<br />

than rant about that fact, a more important issue is their use. An abstract function is<br />

a way of reducing a function to an idea or concept without content. In actual usage,<br />

all this involves is naming the function and placing it in the order you want for your<br />

main algorithm in the template method, not unlike the functions in an interface. In<br />

languages like Java, you can write:<br />

abstract myAbstractFunction( );<br />

and that’s it. In <strong>ActionScript</strong> <strong>3.0</strong>, you can ac<strong>com</strong>plish the same thing using:<br />

function myAbstractFunction( ) {}<br />

So while you don’t have a function that can be designated abstract, you can effectively<br />

create all the abstract functions you want, just as you can develop abstract<br />

classes, with <strong>ActionScript</strong> <strong>3.0</strong>.<br />

Aside from the fact that <strong>ActionScript</strong> <strong>3.0</strong> can create functions that act like abstract<br />

functions, the more important principle is to understand their use in the context of<br />

the Template Method design pattern. Key to all design patterns is the idea of reusability<br />

and flexibility. Abstract functions must be overridden, and therein is their flexibility.<br />

Whenever a function is overridden, it’s changed. By setting up abstract<br />

functions in the parent class, the subclasses can use them as needed to fill out the<br />

particular implementation of the template method algorithm. Reuse is fostered by<br />

the fact that as long as the algorithm template is applicable, the developer has flexibility<br />

because the abstract functions that make up the template method can be modified<br />

to suit the application.<br />

336 | Chapter 9: Template Method Pattern

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

Saved successfully!

Ooh no, something went wrong!