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.

The clearest way we’ve seen <strong>com</strong>position distinguished from inheritance is through<br />

describing the relationship between <strong>com</strong>ponents in an application. If ClassB is subclassed<br />

from ClassA, ClassB is described as being a ClassA. However, if ClassB delegates<br />

to ClassA through <strong>com</strong>position, then ClassB can be said to have a ClassA.<br />

You may have a class set up for loading SWF files in a desired configuration. You<br />

want the functionality of loading the SWF files in that configuration, but you also<br />

want to play audio using MP3 files. Using <strong>com</strong>position, you could simply create a<br />

class that delegates each function to the appropriate classes. Figure 1-7 shows this<br />

relationship:<br />

Has a Loads<br />

SWF files<br />

Has a Loads<br />

MP3 files<br />

Figure 1-7. Delegating to different classes<br />

delegates<br />

delegates<br />

Loads<br />

SWF files<br />

Plays<br />

MP3 files<br />

In this situation, inheritance would not be too helpful. You could subclass one class<br />

but not both. Of course you could instantiate an instance of each class in the third<br />

class, or simply subclass one class and then create an instance of whichever class you<br />

didn’t subclass. That would be a new class with an “is-a” and a “uses-a” different<br />

class for the other functionality. A class is considered a “uses-a” when it creates an<br />

instance of another class but does not hold a reference to it.<br />

To understand and best use <strong>com</strong>position, you need to understand how delegation<br />

really works, and to explain, you need to see it in a slightly more realistic example of<br />

its use. The application will simulate a media application for playing and recording<br />

media files using Flash and Flash Media Server 2 (FMS2). You can play either FLV or<br />

MP3 files using FMS2. While you can record FLV files using FMS2, you can’t publish<br />

MP3 files by themselves. This application is designed for adding future features<br />

and reducing dependencies at the same time.<br />

If you are pretty sure that both the media server and your application will change,<br />

then you’ll want to minimize dependencies by using <strong>com</strong>position and delegation. For<br />

example, suppose that a future version of FMS2 changes, and you can record MP3<br />

files directly. You’d only have to change the RecordAudio class so that it would record<br />

audio. By making that change, nothing else in the application would be affected.<br />

Alternatively, suppose you have a holographic player that you want to add to the<br />

mix. You can easily add another class that will play and/or record holographic<br />

images without disturbing the rest of the application.<br />

52 | 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!