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.

changes are seamlessly spread through the application. If <strong>com</strong>mand objects were not<br />

used and receivers were called directly from multiple invokers, code changes in multiple<br />

locations would be necessary.<br />

Extended Example: Implementing Undo<br />

Another powerful feature of the <strong>com</strong>mand pattern is the clear-cut means it provides<br />

for implementing undo, redo, queuing, and logging features. We all know how valuable<br />

the undo feature is in any productivity application, including games. Because<br />

the <strong>com</strong>mand object encapsulates execution of <strong>com</strong>mands, it can just as easily<br />

encapsulate an undo( ) <strong>com</strong>mand to reverse itself and go back to its previous state.<br />

We need to expand the <strong>com</strong>mand interface to declare an undo( ) <strong>com</strong>mand. However,<br />

before we proceed, let’s stop and think about how to implement this feature.<br />

To implement undo, we need to keep track of executed <strong>com</strong>mands using a <strong>com</strong>mand<br />

stack. A stack is a data structure that’s based on the last-in-first-out (LIFO)<br />

principle. Stacks implement push( ) and pop( ) operations that store and retrieve<br />

items from it. The pop operation always retrieves the last item pushed. This is<br />

exactly what we need to implement undo, as it simply reverses the last <strong>com</strong>mand.<br />

Whenever a <strong>com</strong>mand is executed, its <strong>com</strong>mand object should be pushed into a<br />

stack. Ideally there should be only one <strong>com</strong>mand stack per application. When the<br />

user wants to undo the last <strong>com</strong>mand, the stack should be popped, and the undo( )<br />

<strong>com</strong>mand of the popped <strong>com</strong>mand object should be executed.<br />

An Abstract Interface for Commands<br />

Instead of declaring a pure interface, we will declare an abstract interface for <strong>com</strong>mands<br />

that support undo. We’ll do this to implement the <strong>com</strong>mand stack feature<br />

within the <strong>com</strong>mand class. Example 7-20 shows the abstract interface for the<br />

CommandWithUndo class that implements this. Arrays in <strong>ActionScript</strong> support the push<br />

and pop operations. The <strong>com</strong>mand stack is a static array called aCommandHistory<br />

that’ll hold the <strong>com</strong>mand objects that have already been executed. The default<br />

implementation for the execute( ) method is to push the current <strong>com</strong>mand object<br />

into the <strong>com</strong>mand stack. The undo( ) method has been declared as an abstract<br />

method requiring implementation by subclasses.<br />

Note that <strong>ActionScript</strong> <strong>3.0</strong> language does not support abstract classes. It is up to the<br />

programmer to make sure that classes that need to behave as abstract are subclassed,<br />

and abstract methods implemented.<br />

Example 7-20. CommandWithUndo.as<br />

package<br />

{<br />

// ABSTRACT Class (should be subclassed and not instantiated)<br />

public class CommandWithUndo implements ICommand<br />

266 | Chapter 7: Command Pattern

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

Saved successfully!

Ooh no, something went wrong!