ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Polymorphism | 39

In comparing Example 1-33 with Example 1-34, both theproductDescribe( )and


productPrice( )methods look pretty much the same other than the literal used in the


returnstatement in theproductDescribe( ). In fact, by adding a string parameter to


theproductDescribe( )method, we could make them identical. However, the point


of this application is to demonstrate polymorphism, and so creating different forms


of the methods is intentional.


You can see the practicality of polymorphism by comparing theproductDisplay( )


methods in the two examples. Example 1-33 is set up to play a video and


Example 1-34 a sound. However, note that the identical signatures are maintained in


both examples.


}

}

}

Example 1-34. MP3Player.as


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


public class MP3Player implements IBiz
{
private var sound:Sound;
private var playChannel:SoundChannel;
private var doPlay:URLRequest;
private var MP3Price:Number;


public function productDescribe( ):String
{
return "MP3Player with 1 Terabyte of Memory";
}


public function productPrice(price:Number):String
{
MP3Price=price;
return "$" + MP3Price + "\n";
}


public function productDisplay(song:String):void
{
sound=new Sound( );
playChannel=new SoundChannel( );
doPlay=new URLRequest(song);
sound.load(doPlay);
playChannel=sound.play( );
}
}
}


Example 1-33. Plasma.as (continued)

Free download pdf