* Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
> In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
<br/>
* In plain words:
* Enable the construction of components that are able to delegate, sequence or execute method calls at a time of their choosing;
---
# Explanation
Four terms always associated with the pattern:
* Command
* Object that knows about the receiver and invokes a method of the receiver;
* Receiver
* Object that does the work;
* Invoker
* Knows how to execute a command, and optionally does the bookkeeping about the command execution;
* Client
* Decides which commands to execute at which points, passing the command object to the invoker object;
---
# Example
In our example, a Wizard can cast spells on targets.
* Spell will be the command (implements the Command interface);
* You can express such parameterization in a procedural language with a callback function, that is, a function that's registered somewhere to be called at a later point;
* Commands are an object-oriented replacement for callbacks;
<br/>
* Specify, queue, and execute requests at different times;
* A Command object can have a lifetime independent of the original request;
* If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there;
---
# Applicability
Use the Command pattern when you want to:
* Support undo;
* The Command's execute() operation can store state for reversing its effects in the command itself;
* The Command interface must have an added unexecute() operation that reverses the effects of a previous call to execute;
* Executed commands are stored in a history list;
* Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling unexecute() and execute(), respectively;
---
# Applicability
Use the Command pattern when you want to:
* Support logging changes so that they can be reapplied in case of a system crash;
* By augmenting the Command interface with load() and store() operations, you can keep a persistent log of changes;
* Recovering from a crash involves reloading logged commands from disk and re-executing them with the execute operation;
---
# Applicability
Use the Command pattern when you want to:
* Structure a system around high-level operations build on primitive operations;
* Such a structure is common in information systems that support transactions;
* A transaction encapsulates a set of changes to data;
* The Command pattern offers a way to model transactions;
* Commands have a common interface, letting you invoke all transactions the same way;
* The pattern also makes it easy to extend the system with new transactions
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)