Design Patterns - Command Pattern13 Jul 2018
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
The Client is responsible for creating a Concrete Command and setting it’s receiver. The command object consists of a set of actions on a receiver.
The Receiver knows how to perform the work needed to carry out the request. Any class can act as Receiver.
Command interface declares an interface for all commands. Command is invoked through its
execute() method, which asks for a receiver to perform an action.
The Concrete Command defines a binding between an action and a receiver. The invoker makes a request by calling
execute() and the ConcreteCommand carries it out by calling one or more actions on the Receiver.
- The Command Pattern decouples an object making a request from the one that knows how to perform it.
- A command object is at the center of this decoupling and encapsulates a receiver with an action (or set of actions).
- An invoker makes a request of a Command object by calling its
execute()method, which invokes those actions on the receiver.
- Invoker can be parameterized with Commands, even dynamically at runtime.
- Commands may support undo by implementing an undo method that restores the object to its previous state before the
execute()method was last called.
Tweet Follow @aayushtuladhar