ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Inheritance | 29

Looking at Example 1-19 and theBongoclass, at first you may think that the method


is built incorrectly. It’s wholly different from theGuitarclass in its details, but the


method’s signature is identical.


Remember, when working with interfaces, the number of methods in an interface


can be many or few, but as long as each implementation of the interface includes


every method in the interface and maintains the structure, everything works fine.


You may be wondering where the inheritance is. Given the fact that you must build


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


However, theBandFacesubclasses all inherit its interfaces. So essentially, the sub-


classes inherit the interface but not the implementation.


Finally, to test the application, theMakeSoundclass, listed in Example 1-20, tests both


classes and their very different constructions of the single method from theBandFace


public function playInstrument(strum:String):void
{
trace("Playing my air "+ strum);
}
}
}


Example 1-19. Bongo.as


package
{
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;


public class Bongo implements BandFace
{


public function Bongo( ){}


private var sound:Sound;
private var playNow:SoundChannel;
private var doPlay:URLRequest;


public function playInstrument(strum:String):void
{
sound=new Sound( );


doPlay=new URLRequest(strum);
sound.load(doPlay);
playNow=sound.play( );
}
}
}


Example 1-18. Guitar.as (continued)

Free download pdf