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.

Example 1-18. Guitar.as (continued)<br />

}<br />

}<br />

public function playInstrument(strum:String):void<br />

{<br />

trace("Playing my air "+ strum);<br />

}<br />

Looking at Example 1-19 and the Bongo class, at first you may think that the method<br />

is built incorrectly. It’s wholly different from the Guitar class in its details, but the<br />

method’s signature is identical.<br />

Example 1-19. Bongo.as<br />

package<br />

{<br />

import flash.media.Sound;<br />

import flash.media.SoundChannel;<br />

import flash.net.URLRequest;<br />

}<br />

public class Bongo implements BandFace<br />

{<br />

}<br />

public function Bongo( ){}<br />

private var sound:Sound;<br />

private var playNow:SoundChannel;<br />

private var doPlay:URLRequest;<br />

public function playInstrument(strum:String):void<br />

{<br />

sound=new Sound( );<br />

}<br />

doPlay=new URLRequest(strum);<br />

sound.load(doPlay);<br />

playNow=sound.play( );<br />

Remember, when working with interfaces, the number of methods in an interface<br />

can be many or few, but as long as each implementation of the interface includes<br />

every method in the interface and maintains the structure, everything works fine.<br />

You may be wondering where the inheritance is. Given the fact that you must build<br />

all the interface’s methods, it looks more like a customization than an inheritance.<br />

However, the BandFace subclasses all inherit its interfaces. So essentially, the subclasses<br />

inherit the interface but not the implementation.<br />

Finally, to test the application, the MakeSound class, listed in Example 1-20, tests both<br />

classes and their very different constructions of the single method from the BandFace<br />

Inheritance | 29

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

Saved successfully!

Ooh no, something went wrong!