10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example 10-3. StopState.as<br />

1 package<br />

2 {<br />

3 //Stop State;<br />

4 class StopState implements State<br />

5 {<br />

6 var videoWorks:VideoWorks;<br />

7 public function StopState(videoWorks:VideoWorks)<br />

8 {<br />

9 trace("--Stop State--");<br />

10 this.videoWorks=videoWorks;<br />

11 }<br />

12 public function startPlay( ):void<br />

13 {<br />

14 trace("Begin playing");<br />

15 videoWorks.setState(videoWorks.getPlayState( ));<br />

16 }<br />

17 public function stopPlay( ):void<br />

18 {<br />

19 trace("You're already stopped");<br />

20 }<br />

21 }<br />

22 }<br />

By adding a VideoWorks instance, we have a way to access the getter and setter methods<br />

in each state. Line 15 invokes the VideoWorks instance to change the state to the<br />

Play state.<br />

Next, we’ll do the same thing with the Play state shown in Example 10-4. Save the<br />

following as PlayState.as.<br />

Example 10-4. PlayState.as<br />

1 package<br />

2 {<br />

3 //Play State<br />

4 class PlayState implements State<br />

5 {<br />

6 var videoWorks:VideoWorks;<br />

7 public function PlayState(videoWorks:VideoWorks)<br />

8 {<br />

9 trace("--Play State--");<br />

10 this.videoWorks=videoWorks;<br />

11 }<br />

12 public function startPlay( ):void<br />

13 {<br />

14 trace("You're already playing");<br />

15 }<br />

16 public function stopPlay( ):void<br />

17 {<br />

18 trace("Stop playing.");<br />

19 videoWorks.setState(videoWorks.getStopState( ));<br />

20 }<br />

21 }<br />

22 }<br />

Minimalist Abstract State Pattern | 365

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!