Extended Example: Implementing Undo | 267
Concrete Commands that Implement Undo
Now we will re-implement the increment and decrement concrete commands to the
abstract interface declared byCommandWithUndo. The two new concrete command
classes areIncrementCommandWithUndo(Example 7-21) andDecrementCommandWithUndo
(Example 7-22). To implement the undo feature, we primarily need to push all exe-
cuted command objects into the command stack. Theexecute( )method does this
by calling theexecute( )method in the superclass in the last statement (line 17), and
implementing theundo( )method. Theundo( )method simply reverses the effects of
theexecute( ) method (line 20).
{
internal static var aCommandHistory:Array = new Array( );
public function execute( ):void
{
aCommandHistory.push(this);
}
// ABSTRACT Method (must be overridden in a subclass)
public function undo( ):void {}
}
}
Example 7-21. IncrementCommandWithUndo.as
1 package
2 {
3 import flash.text.TextField;
4
5 class IncrementCommandWithUndo extends CommandWithUndo
6 {
7 var receiver:TextField;
8
9 public function IncrementCommandWithUndo(rec:TextField):void
10 {
11 this.receiver = rec;
12 }
13
14 override public function execute( ):void
15 {
16 receiver.text = String(Number(receiver.text) + 1);
17 super.execute( );
18 }
19
20 override public function undo( ):void
21 {
Example 7-20. CommandWithUndo.as (continued)