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.

In reviewing how the process works, first, the showTime instance is typed to the interface,<br />

IVid. Next, showTime instantiates the VidPlayer class that has all the details for<br />

playing the video. By doing so, it inherits the Sprite class as well as the IVid interface.<br />

Then the showTime client plays the video using the playVid( ) method. Finally,<br />

when the showTime instance is added to the display list with the addChild statement, it<br />

is added as both the child of the VidPlayer class and the DisplayObject class by using<br />

the displayObject getter. Because the getter, displayObject, is included in the display<br />

list, you will not get the following error:<br />

1067: Implicit coercion of a value of type IVid to an unrelated type<br />

flash.display:DisplayObject.<br />

IVid appears in the error because the instance was typed to the interface instead of<br />

the implementation. By slipping in the DisplayObject typed getter method, we avoid<br />

the error.<br />

Favor Composition<br />

Throughout this chapter, we have discussed the principle of programming to the<br />

interface instead of the implementation. The second principle of object-oriented<br />

design posited by Gamma, Helm, Johnson and Vlissdes (GoF) is: Favor object <strong>com</strong>position<br />

over class inheritance.<br />

To understand this second key principle, we need to understand exactly what <strong>com</strong>position<br />

means, and its advantages over inheritance. After all, the principle is essentially<br />

stating that your programs will be better using <strong>com</strong>position than inheritance.<br />

Does this mean to abandon inheritance? After all, inheritance is one key concept in<br />

object-oriented programming. Actually, what GoF discuss is that most programmers<br />

rely too heavily on inheritance, and need to use it more in conjunction with <strong>com</strong>position.<br />

Most programmers create new objects from old objects using inheritance, and,<br />

through <strong>com</strong>position, the old and new objects can be used together.<br />

The best way to understand <strong>com</strong>position is to see it in the context of its use. In this<br />

book, both the State and Strategy patterns are built using <strong>com</strong>position, and they<br />

depend on delegation. In the State design pattern, an object delegates requests to an<br />

object representing the current state in an application. In the Strategy design pattern,<br />

specific requests are delegated to objects representing strategies (algorithms) for<br />

solving problems. The great advantage of both these design patterns is that they are<br />

flexible and not bogged down in inflexible dependencies.<br />

Favor Composition | 49

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

Saved successfully!

Ooh no, something went wrong!