Definition
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
UML Class Diagram
Command: when and where use it
- The Command design pattern encapsulates an action or a request as an object. The classic usage of this pattern is a menu system where each command object represents an action and an associated undo action. Menu actions include menu items such as File | Open, File | Save, Edit | Copy, etc each of which gets mapped to its own command object.
- All Commands implement the same interface, so they can be handled polymorphically. Typically their interface includes methods such as Do and Undo (or Execute and Undo). Areas where you find Command patterns are: menu command systems and in applications that require undo functionality (word processors, and sometimes in business applications that need database undo functionality).
Sample
- Command Interface
- Command class
- Receiver class
- Invoke class
Ref: Gang of Four tutorial
No comments:
Post a Comment