05 Dec 2023
Intermediate
The Command design pattern is a behavioral pattern that turns a request into a standalone object, encapsulating all information about the request. This transformation allows for parameterization of clients with different requests, queuing of requests, and logging of the parameters, as well as providing support for undoable operations. In C#, the Command pattern typically involves the following main components:
-
Command Interface:
- Declares an interface with a method (often named
Execute) that concrete command classes must implement.
public interface ICommand { void Execute(); } - Declares an interface with a method (often named
-
Concrete Command:
- Implements the
ICommandinterface. - Holds a reference to the receiver and invokes a specific operation on it when
Executeis called.
public class ConcreteCommand : ICommand { private Receiver receiver; public ConcreteCommand(Receiver receiver) { this.receiver = receiver; } public void Execute() { receiver.Action(); } } - Implements the
-
Receiver:
- Knows how to perform the operations associated with a request.
public class Receiver { public void Action() { Console.WriteLine("Receiver's Action method called"); } } -
Invoker:
- Asks the command to execute the request.
- Does not know the specific operation the command will perform, only that it implements the
ICommandinterface.
public class Invoker { private ICommand command; public void SetCommand(ICommand command) { this.command = command; } public void ExecuteCommand() { command.Execute(); } } -
Client:
- Creates command objects and associates them with receivers.
- Sets the command for the invoker and triggers the execution of the command.
class Client { static void Main() { Receiver receiver = new Receiver(); ICommand command = new ConcreteCommand(receiver); Invoker invoker = new Invoker(); invoker.SetCommand(command); invoker.ExecuteCommand(); } }
In this example:
ICommandis the interface that declares theExecutemethod, which concrete command classes must implement.ConcreteCommandis a concrete implementation of theICommandinterface. It holds a reference to aReceiverand invokes a specific operation on it whenExecuteis called.Receiveris the class that knows how to perform the operations associated with a request.Invokeris the class that asks the command to execute the request. It does not know the specific operation the command will perform, only that it implements theICommandinterface.- The
Clientcreates a receiver, a concrete command, and an invoker. It associates the command with the receiver, sets the command for the invoker, and triggers the execution of the command.
The Command pattern decouples the sender and receiver of a request by turning the request into a self-contained object, allowing for more flexibility and extensibility in the design. It also supports features like undo operations and command queues.
software-design-patterns
command-design-pattern