ActionScript 3.0 Design Patterns

(Chris Devlin) #1

272 | Chapter 7: Command Pattern


Now that the utility classes have been created, we can develop the command pattern


elements for the application.


Creating a Command to Play a Podcast


The command interface will be the sameICommandclass defined in Example 7-1. The


concrete commandwill be thePlayPodcastCommandclass shown in Example 7-27. The


constructor takes two parameters, the receiver of typeRadio,and the URL of the


podcast as typeString.


Developing the Radio Receiver


Thereceiverclass shown in Example 7-28 is calledRadioand subclassesSprite.It


uses theTextDisplayFieldclass (see Example 7-26) from the previously developed


utilspackage to display a text field to show the currently playing podcast item (lines


20-21). TheaudioDisplayproperty references the text field. In addition, it declares a


static property calledaudioChannelof typeSoundChannel(line 15). The reason the


sound channel is declared asstaticis to make sure that only one podcast plays at a


var format:TextFormat = new TextFormat("Verdana");
format.size = fontSize;
setTextFormat(format);
}
}
}


Example 7-27. PlayPodcastCommand.as


package
{
class PlayPodcastCommand implements ICommand
{
var receiver:Radio;
var podCastURL:String;


public function PlayPodcastCommand(rec:Radio, url:String):void
{
this.receiver = rec;
this.podCastURL = url;
}


public function execute( ):void
{
this.receiver.playPodcast(this.podCastURL);
}
}
}


Example 7-26. TextDisplayField.as (continued)

Free download pdf