ActionScript 3.0 Design Patterns

(Chris Devlin) #1

262 | Chapter 7: Command Pattern


The Increment and Decrement Commands


Now that ourInvokerPanelis complete, we can develop the command classes to


increment and decrement a value in a text field. Examples 7-16 and 7-17 show the


IncrementCommand and DecrementCommand classes, both of which implement the


ICommandinterface (Example 7-1). Note that the receiver is the built-inTextField


class and the text in the field is assigned using itstextproperty. Theexecute( )


method gets the text value from the receiver, casts it to a Number, and assigns the


manipulated value back to the receiver.


41 }

42 }

43 }

Example 7-16. IncrementCommand.as


package {


import flash.text.TextField;


class IncrementCommand implements ICommand {


var receiver:TextField;


public function IncrementCommand(rec:TextField):void {
this.receiver = rec;
}


public function execute( ):void {
receiver.text = String(Number(receiver.text) + 1);
}
}
}


Example 7-17. DecrementCommand.as


package {


import flash.text.TextField;


class DecrementCommand implements ICommand {


var receiver:TextField;


public function DecrementCommand(rec:TextField):void {
this.receiver = rec;
}


public function execute( ):void {
receiver.text = String(Number(receiver.text) – 1);


Example 7-15. InvokerPanel.as (continued)

Free download pdf