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.

your client dependency leads to unexpected results or fails to run altogether. By<br />

depending on interfaces, your code is decoupled from the implementation, allowing<br />

variation in the implementation. This does not mean you get rid of dependency, but<br />

instead you just manage it more flexibly. A key element of this approach is to separate<br />

the design from the implementation. By doing so, you separate the client from<br />

the implementation as well.<br />

As we have seen, you can use the <strong>ActionScript</strong> <strong>3.0</strong> interface structure or an abstract<br />

class to set up this kind of flexible dependence. However, when you use either, you<br />

must be aware of the way in which to manage the dependency. If you use an<br />

interface structure, any change in the interface will cause failure in all the clients<br />

that use the interface. For example, suppose you have five methods in an interface.<br />

You decide that you need two more. As soon as you add the two new methods to<br />

your interface, your client is broken. So, if you use the interface structure, you must<br />

treat the interface as set in stone. If you want to add new functionality, simply create<br />

a new interface.<br />

The alternative to using the interface structure is to use abstract classes. While the<br />

abstract class structure is not supported in <strong>ActionScript</strong> <strong>3.0</strong> as interfaces are, you can<br />

easily create a class to do everything an abstract class does. As we saw in several<br />

examples in this chapter beginning with Example 1-3, creating and using an abstract<br />

class is simply adhering to the rules that abstract classes follow anyway. For example,<br />

abstract classes are never directly implemented.<br />

The advantage of an abstract class over an interface is that you won’t destroy a client<br />

when you add methods to the base class. All the abstract function must be overridden<br />

to be used in a unique manner, but if your client has no use for a new method,<br />

by doing nothing, the method is inherited but not employed or changed. On the<br />

other hand, every single method in an interface structure must be implemented.<br />

A further advantage of an abstract class is that you can add default behaviors and<br />

even set up concrete methods inherited by all subclasses. Of course the downside of<br />

default behaviors and concrete methods is that a subclass may not want or need the<br />

default or concrete methods, and a client may end up doing something unwanted<br />

and unexpected if any concrete changes are introduced. Whatever the case, though,<br />

management of dependency is easier with the flexibility offered by interfaces and<br />

abstract classes over concrete classes and methods.<br />

So the decision of whether to use an interface or abstract class depends on what you<br />

want your design to do. If the ability to add more behaviors easily is most important,<br />

then abstract classes are a better choice. Alternatively, if you want independence from<br />

the base class, then choose an interface structure. No matter what you do, though,<br />

you need to think ahead beyond the first version of your application. If your application<br />

is built with an eye to future versions and possible ways that it can expand or<br />

change, you can better judge what design pattern would best achieve your goals.<br />

46 | Chapter 1: Object-Oriented Programming, <strong>Design</strong> <strong>Patterns</strong>, and <strong>ActionScript</strong> <strong>3.0</strong>

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

Saved successfully!

Ooh no, something went wrong!