ActionScript 3.0 Design Patterns

(Chris Devlin) #1

278 | Chapter 7: Command Pattern


Commands to Dynamically Assign Command Objects


To dynamically assign command objects, we need to create two new concrete com-


mands to set the podcast genre to either music or news. This is accomplished by the


SetToMusicGenreCommand(Example 7-32) andSetToNewsGenreCommand(Example 7-33)


classes.


15 this.musicPodcastCommands = new Array(3);
16 }
17
18 public function setGenre(genre:uint)
19 {
20 if (genre == NEWS)
21 {
22 this.currentGenre = NEWS;
23 } else if (genre == MUSIC) {
24 this.currentGenre = MUSIC;
25 }
26 this.updateCommandButtons( );
27 }
28
29 public function setGenreCommand(nSlot:int, c:ICommand, genre:uint):void
30 {
31 if (genre == NEWS)
32 {
33 this.newsPodcastCommands[nSlot] = c;
34 } else if (genre == MUSIC) {
35 this.musicPodcastCommands[nSlot] = c;
36 }
37 this.updateCommandButtons( );
38 }
39
40 private function updateCommandButtons( )
41 {
42 for (var i:int = 0; i < 3; i++)
43 {
44 if (currentGenre == NEWS)
45 {
46 this.commandList[i] = this.newsPodcastCommands[i];
47 } else if (currentGenre == MUSIC) {
48 this.commandList[i] = this.musicPodcastCommands[i];
49 }
50 }
51 }
52 }
53 }

Example 7-32. SetToMusicGenreCommand.as


package
{


Example 7-31. DynamicControlButtons.as

Free download pdf