ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Example: Podcast Radio | 275

The Client Assigns Podcasts to Push Buttons


In Example 7-30, the client first creates the receiver and adds it to the display list


(lines 1-5). It then creates the push buttons that represent the invoker. The buttons’


labels correspond to the podcast’s genre. Finally, the concrete command objects are


created, and assigned to the corresponding buttons in the invoker (lines 25-29). The


PlayPodcastCommandclass constructor takes the podcast URL as a parameter in addi-


tion to the receiver instance. The client code can be run from thedocument classof


the Flash document.


var btn:TextButton = new TextButton(sName);
this.buttonList[nSlot] = btn;
btn.x = nSlot * 100;
btn.addEventListener(MouseEvent.CLICK,
this.buttonClicked);
this.addChild(btn);
}


private function buttonClicked(e:Event)
{
for (var i:int = 0; i < buttonList.length; i++)
{
if (buttonList[i] === e.target)
{
this.commandList[i].execute( );
break;
}
}
}
}
}


Example 7-30. Client code for the podcast radio


1 // create radio (receiver)
2 var radio:Radio = new Radio( );
3 radio.x = 50;
4 radio.y = 50;
5 this.addChild(radio);
6
7 // create control buttons (invoker)
8 var controls:ControlButtons = new ControlButtons( );
9 controls.setButton(0,"News");
10 controls.setButton(1,"Music");
11 controls.setButton(2,"Technology");
12 controls.setButton(3,"Business");
13 controls.setButton(4,"Sports");
14 controls.x = 50;
15 controls.y = this.stage.stageHeight - 50;
16 this.addChild(controls);

Example 7-29. ControlButtons.as (continued)

Free download pdf