Command Pattern
- is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as method arguments, delay or queue a request’s execution, and support undoable operations
Code Structure
|
|
Code Example
Click here to expand...
interface Command { void execute(); }class ConcreteCommand1 implements Command { void execute() { // logic 1 } } class ConcreteCommand2 implements Command { void execute() { // logic 2 } }interface CommandInvoker { setCommand(Command command); executeCommand(); }abstract class BaseCommandInvoker implements CommandInvoker { Command command; setCommand(Command command) { this.command = command; } executeCommand() { command.execute(); } } class Button_ConcreteCommandInvoker1 extends BaseCommandInvoker { Receiver receiver; Button_ConcreteCommandInvoker1(Receiver receiver, params) { this.receiver = receiver; } executeCommand() { receiver.operation(a, b, c); } } class Scroll_ConcreteCommandInvoker2 extends BaseCommandInvoker { // modify as needed }main() { Command command1 = new ConcreteCommand1(); Command command2 = new ConcreteCommand2(); Receiver receiver = new Receiver(); CommandInvoker commandInvoker1 = new Button_ConcreteCommandInvoker1(receiver); CommandInvoker commandInvoker1 = new Scroll_ConcreteCommandInvoker2(); commandInvoker1.setCommand(command1); commandInvoker2.setCommand(command2); // execute command commandInvoker1.execute(); commandInvoker2.execute(); }
Comparisons
Click here to expand...
Link to originalChain of Responsibility, Command, Mediator, and Observer address various ways of connecting senders and receivers of requests:
- Chain of Responsibility passes a request sequentially along a dynamic chain of potential receivers until one of them handles it
- Command establishes unidirectional connections between senders and receivers
- Mediator eliminates direct connections between senders and receivers, forcing them to communicate indirectly via a mediator object
- Observer lets receivers dynamically subscribe to and unsubscribe from receiving requests
Link to originalChain of Responsibility Pattern vs Command Pattern
- Handlers in the Chain of Responsibility Pattern can be implemented as Commands. In this case, you can execute a lot of different handler-command-operations over the same request-context object
- Requests in the Chain of Responsibility Pattern can be implemented as Commands. In this case, you can execute the same request-command-operation through a series of different handler-contexts
Link to originalCommand Pattern vs Prototype Pattern
- Prototype Pattern can help when you need to save copies of Commands into history
Link to originalCommand and Strategy may look similar because you can use both to parameterize an object with some action. However, they have very different intents.
- Command Pattern can be used to convert any operation into an object. The operation’s parameters become fields of that object. The conversion lets you defer execution of the operation, queue it, store the history of commands, send commands to remote services, etc
- Strategy Pattern usually describes different ways of doing the same thing, letting you swap these algorithms within a single context class
Link to originalCommand Pattern vs Visitor Pattern
- You can treat Visitor Pattern as a powerful version of the Command Pattern. Its objects can execute operations over various objects of different classes.
Link to original
- Command and Memento can be used together when implementing “undo”. In this case, commands are responsible for performing various operations over a target object, while mementos save the state of that object just before a command gets executed.
/behavioral-design-patterns/command-pattern/structure-indexed-2x.png)