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.

works fine. What’s more, using the spaceWarrior instance, we could redefine it as an<br />

Alien( ) instance, and it’d still work fine. Change the last part to:<br />

//Program to interface<br />

var spaceWarrior:SpaceWarrior=new Earthling( );<br />

spaceWarrior.useWeapon( );<br />

spaceWarrior=new Alien( );<br />

spaceWarrior.useWeapon( );<br />

Now, the output shows:<br />

Zaaaapp!!!<br />

Ka Boom!!!<br />

Zaaaapp!!!<br />

As you can see, by programming to the interface, the instances are far more flexible.<br />

We don’t have to know what’s in the function (interface) or what it’ll do, we just<br />

know that it will do what it’s supposed to, depending on the constructor we use.<br />

Object Composition<br />

To see why object <strong>com</strong>position is favored over class inheritance, we need to see what<br />

each does. Previous chapters have used both object <strong>com</strong>position and inheritance,<br />

and so you may have some idea of what each does. Both concepts were introduced in<br />

Chapter 1, but because the Observer design pattern clearly illustrates the use of <strong>com</strong>position,<br />

we are reviewing it here again.<br />

Keeping in mind that the <strong>com</strong>parison between inheritance and object <strong>com</strong>position<br />

relates to building flexible and reusable software elements, we have a base for <strong>com</strong>parison.<br />

So we can restate the principle as:<br />

For improved flexible and reusable software, favor object <strong>com</strong>position over class<br />

inheritance.<br />

Reusable software developed through inheritance is relatively straightforward. Developers<br />

take an existing class and use it again with the particulars created through subclasses.<br />

Because the internals of the objects are often visible to the subclasses, the<br />

term white-box reuse is applied to this implementation.<br />

Object <strong>com</strong>position, by contrast, achieves new functionality by bringing together<br />

existing objects. The <strong>com</strong>position process can be understood as one class using<br />

another class’s functionality, whereas inheritance depends on the class having the<br />

functionality. So, in <strong>com</strong>position, one class uses a functionality of another class,<br />

while in inheritance, a sub class is-a class with functionality inherited from another<br />

class. Because object <strong>com</strong>position hides the internal details, the term black-box reuse<br />

is applied to this kind of development.<br />

Inheritance has some advantages in being simple to create, visible, and relatively simple<br />

for making certain types of changes. For example, if you want a change to be<br />

288 | Chapter 8: Observer Pattern

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

Saved successfully!

Ooh no, something went wrong!