The Command pattern is used to encapsulate a request as an object, allowing you to parameterize clients with different requests, queues, or undoable operations. It is useful when you want to separate the logic of executing commands, for example, in a text editor that allows you to undo and redo actions like copy, paste, or delete.
C# Code Example:
// Interface for commands
public interface ICommand
{
void Execute();
}
// Class representing a receiver, the one who actually performs actions
public class Light
{
public void TurnOn()
{
Console.WriteLine("The light is on.");
}
public void TurnOff()
{
Console.WriteLine("The light is off.");
}
}
// Command to turn on the light
public class TurnOnLightCommand : ICommand
{
private Light _light;
public TurnOnLightCommand(Light light)
{
_light = light;
}
public void Execute()
{
_light.TurnOn();
}
}
// Command to turn off the light
public class TurnOffLightCommand : ICommand
{
private Light _light;
public TurnOffLightCommand(Light light)
{
_light = light;
}
public void Execute()
{
_light.TurnOff();
}
}
// The invoker, which calls the commands
public class RemoteControl
{
private ICommand _command;
public void SetCommand(ICommand command)
{
_command = command;
}
public void PressButton()
{
_command.Execute();
}
}
class Program
{
static void Main(string[] args)
{
// Create objects
Light light = new Light();
ICommand turnOn = new TurnOnLightCommand(light);
ICommand turnOff = new TurnOffLightCommand(light);
// Create the invoker (remote control)
RemoteControl remote = new RemoteControl();
// Turn the light on
remote.SetCommand(turnOn);
remote.PressButton(); // Output: The light is on.
// Turn the light off
remote.SetCommand(turnOff);
remote.PressButton(); // Output: The light is off.
}
}
Code Explanation:
In this example, the RemoteControl is the invoker that calls commands to turn the light on or off. The TurnOnLightCommand and TurnOffLightCommand encapsulate the actions of turning the light on and off, and the remote control decides which command to execute. This separates the logic of the remote control from the specifics of each command.
Conclusion:
The Command pattern is useful for encapsulating actions as independent objects, allowing greater flexibility to define, replace, or even undo operations. It separates the client requesting an action from the object performing that action.
Source code: GitHub
Top comments (0)