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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 7-20. CommandWithUndo.as (continued)<br />

}<br />

{<br />

}<br />

internal static var aCommandHistory:Array = new Array( );<br />

public function execute( ):void<br />

{<br />

aCommandHistory.push(this);<br />

}<br />

// ABSTRACT Method (must be overridden in a subclass)<br />

public function undo( ):void {}<br />

Concrete Commands that Implement Undo<br />

Now we will re-implement the increment and decrement concrete <strong>com</strong>mands to the<br />

abstract interface declared by CommandWithUndo. The two new concrete <strong>com</strong>mand<br />

classes are IncrementCommandWithUndo (Example 7-21) and DecrementCommandWithUndo<br />

(Example 7-22). To implement the undo feature, we primarily need to push all executed<br />

<strong>com</strong>mand objects into the <strong>com</strong>mand stack. The execute( ) method does this<br />

by calling the execute( ) method in the superclass in the last statement (line 17), and<br />

implementing the undo( ) method. The undo( ) method simply reverses the effects of<br />

the execute( ) method (line 20).<br />

Example 7-21. IncrementCommandWithUndo.as<br />

1 package<br />

2 {<br />

3 import flash.text.TextField;<br />

4<br />

5 class IncrementCommandWithUndo extends CommandWithUndo<br />

6 {<br />

7 var receiver:TextField;<br />

8<br />

9 public function IncrementCommandWithUndo(rec:TextField):void<br />

10 {<br />

11 this.receiver = rec;<br />

12 }<br />

13<br />

14 override public function execute( ):void<br />

15 {<br />

16 receiver.text = String(Number(receiver.text) + 1);<br />

17 super.execute( );<br />

18 }<br />

19<br />

20 override public function undo( ):void<br />

21 {<br />

Extended Example: Implementing Undo | 267

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

Saved successfully!

Ooh no, something went wrong!