The command pattern is a behavioral design pattern used in object-oriented programming which encapsulates a request as an object, thereby allowing for parameterization of clients with queues, requests, and operations. It also allows for the support of undoable operations. The command pattern decouples the object that invokes the operation from the one that knows how to perform it.
Here's a breakdown of the components involved in the command pattern:
- Command Interface:
This typically involves defining an interface with a method that represents the execution of the command. For example, it might be a simple
Execute() method. Every command will implement this interface.
public interface ICommand
{
void Execute();
}
- Concrete Command:
Each specific command is a class that implements the
ICommand interface. It defines the binding between the receiver of the command and an action.
public class ConcreteCommand : ICommand
{
private readonly Receiver _receiver;
public ConcreteCommand(Receiver receiver)
{
_receiver = receiver;
}
public void Execute()
{
_receiver.Action();
}
}
- Receiver:
This is the object that knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver.
public class Receiver
{
public void Action()
{
// Actual implementation to perform the command
}
}
- Invoker:
This is the object that knows how to execute a command but doesn’t know how the command has been implemented. It only knows about the command interface.
public class Invoker
{
private ICommand _command;
public void SetCommand(ICommand command)
{
_command = command;
}
public void ExecuteCommand()
{
_command.Execute();
}
}
- Client:
The client decides which commands to execute at which points. It associates the command with the invoker.
public class Client
{
void Main()
{
var receiver = new Receiver();
var command = new ConcreteCommand(receiver);
var invoker = new Invoker();
invoker.SetCommand(command);
invoker.ExecuteCommand();
}
}
When to use the Command Pattern:
- When you need to parameterize objects by an action to perform.
- When you need to implement undo/redo functionality.
- When you want to implement operational transactions that can be committed or rolled back.
- When you want to queue, log, or network-send requests.
The benefits of using the command pattern include:
- Decoupling the sender and receiver: The invoker is not aware of the receiver's operations, enabling you to vary receivers and commands independently.
- Extension: Adding new commands is easy and doesn’t require changing the existing code, which helps to maintain the Open/Closed Principle.
- Composition: Commands can be composed into a composite command, which can execute multiple commands in a particular sequence.
- Flexibility: Commands can be managed, queued, and executed at an arbitrary time.
The command pattern is an elegant way to represent and encapsulate all the information needed to perform an action or trigger an event at a later time. This includes the method name, the object that owns the method, and any parameters linked to the method.