Chapter 10. Command Pattern


In This Chapter

Understanding the Command Pattern 137

Building a Simple Command Application 139

Making Commands Undoable and keeping Command histories 146

Building an Undoable Application 148

Building a Redoable Application 153

Using Commands to Build a Proximity game 154

Summary 168

The Command pattern encapsulates functionality into a class. Although the pattern might appear simplistic, don't let its simplicity deceive you. The Command pattern is a powerful way for enabling a range of features that would be difficult to implement using a different approach. Here are some of the most common uses of the Command pattern:

  • Building highly reusable components. The Command pattern decouples the functionality from the initiator. Components that use Command pattern objects are highly extensible because they can employ any object that implements the correct interface.

  • Queuing requests. When operations must occur in sequence, they must be queued. Because the Command pattern encapsulates the request into an object, it's possible to queue the requests by placing them in an array or similar collection.

  • Supporting undo and redo. Because Command pattern objects can be stored in memory, it's possible to keep a history of which operations have occurred. By implementing an undo() and/or redo() method, it's possible to add undo and redo features to an application without difficulty.

  • Making transactional or "wizard" operations. An operation often consists of many steps. If any step fails or if the user cancels at any point, it is necessary to roll back any changes that have occurred. The Command pattern enables you to defer execution or to roll back operations by calling the undo() method of a succession of objects.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net