268 | Chapter 7: Command Pattern
TheDecrementCommandWithUndo class is similar, and shown in Example 7-22.
We also need a new command object that’ll be attached to an undo button on the
invoker. Example 7-23 shows theUndoLastCommandclass that will undo the last opera-
tion. Theexecute( )method first checks if theaCommandHistoryarray contains any
command objects, and pops the array to get the most recently executed command. It
then proceeds to call theundo( )method on the popped command object. Note that
the undo command does not push itself into the command stack. It also throws an
IllegalOperationError exception if itsundo( ) method is called.
22 receiver.text = String(Number(receiver.text) – 1);
23 }
24 }
25 }
Example 7-22. DecrementCommandWithUndo.as
package
{
import flash.text.TextField;
class DecrementCommandWithUndo extends CommandWithUndo
{
var receiver:TextField;
public function DecrementCommandWithUndo(rec:TextField):void
{
this.receiver = rec;
}
override public function execute( ):void
{
receiver.text = String(Number(receiver.text) – 1);
super.execute( );
}
override public function undo( ):void
{
receiver.text = String(Number(receiver.text) + 1);
}
}
}
Example 7-23. UndoLastCommand.as
package
{
import flash.errors.IllegalOperationError;
class UndoLastCommand extends CommandWithUndo
Example 7-21. IncrementCommandWithUndo.as