ActionScript 3.0 Design Patterns

(Chris Devlin) #1

56 | Chapter 1: Object-Oriented Programming, Design Patterns, and ActionScript 3.0


Create a Flash document, save it in the same folder with the class files, and, in the


Document class window, type inTestMedia. When you test it, the Output window


simulates the behaviors.


Playing my video. Look at that!
I'm recording this tornado live! Holy....crackle, crackle

My MP3 is cranking out great music!
Rats! I can't record MP3 by itself.

The algorithms you’d have in an actual application would be more comple xthan the


simpletracestatements. However, no matter how comple xthey got, the dependen-


cies are set up so that a change in one would only affect those elements in the appli-


cation that you want to change, and not those you don’t want to change.


Making Composition, Inheritance, and Instantiation Work


Together


We haven’t compared the relative advantages and disadvantages between composi-


tion and inheritance because with composition, both composition and inheritance


operate in the same environment. For that matter, so too does instantiation where


one class simply instantiates an instance of another class. The idea that one would


work with one and exclude the other was never the point made by GoF in their prin-


ciple tofavor composition over inheritance. Yes, stated that way, that’s what the prin-


ciple sounds like. However, in explaining the principle, the founders of design


patterns not only explicitly point out that composition and inheritance work


together, their design patterns show it.


The application in Example 1-45 through Example 1-54 was used to illustrate com-


position. It also shows inheritance and instantiation at work. To see this relationship


better, consider Figure 1-8.


In Figure 1-8, you can see that theMediaclassdelegatesto theRecordMediaclass. It


does this by holding a reference to that class in its definition. (See Example 1-45.)


var recordMedia:RecordMedia;

In theVideoFlashclass, you can see that itinheritsfrom theMediaclass. At the same


time, though,VideoFlashinstantiatesRecordVideo.


recordMedia = new RecordVideo( );

var delAudio:Media = new Mp3( );
delAudio.doPlayMedia( );
delAudio.doRecordMedia( );
}
}
}


Example 1-54. TestMedia.as (continued)

Free download pdf