Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Command design pattern.
The Command design pattern is a behavioral design pattern that encapsulates a request as an object and separates the sender of the request from the receiver of the request. In simpler terms, it allows you to create a "Command" object that contains all the information necessary to perform a specific action or request, which can then be executed. The Command pattern allows for the creation of reusable commands that can be easily modified or combined to create more complex behaviors.
The Command design pattern consists of the following components:
Below is an example of the Command pattern in C#:
// Receiver: Light
public class Light
{
public void TurnOn()
{
Console.WriteLine("Light is turned on");
}
public void TurnOff()
{
Console.WriteLine("Light is turned off");
}
}
// Command interface
public interface ICommand
{
void Execute();
}
// Concrete command: TurnOnCommand
public class TurnOnCommand : ICommand
{
private readonly Light _light;
public TurnOnCommand(Light light)
{
_light = light;
}
public void Execute()
{
_light.TurnOn();
}
}
// Concrete command: TurnOffCommand
public class TurnOffCommand : ICommand
{
private readonly Light _light;
public TurnOffCommand(Light light)
{
_light = light;
}
public void Execute()
{
_light.TurnOff();
}
}
// Invoker: Switch
public class Switch
{
private ICommand _turnOnCommand;
private ICommand _turnOffCommand;
public Switch(ICommand turnOnCommand, ICommand turnOffCommand)
{
_turnOnCommand = turnOnCommand;
_turnOffCommand = turnOffCommand;
}
public void TurnOn()
{
_turnOnCommand.Execute();
}
public void TurnOff()
{
_turnOffCommand.Execute();
}
}
// Example usage
var light = new Light();
var turnOnCommand = new TurnOnCommand(light);
var turnOffCommand = new TurnOffCommand(light);
var switchObj = new Switch(turnOnCommand, turnOffCommand);
switchObj.TurnOn(); // Output: "Light is turned on"
switchObj.TurnOff(); // Output: "Light is turned off"
In this example, the Light class acts as a Receiver and provides methods to turn it on and off. The TurnOnCommand and TurnOffCommand classes are commands that implement the ICommand interface and receive a Light object in their constructor.
The Switch class acts as an Invoker and receives the commands it is going to execute through the constructor method.
When TurnOn is called on the Switch object, it executes TurnOnCommand, which calls the TurnOn method on the Light object, resulting in the message "Light is turned on" being printed to the console. Similarly, when TurnOff is called, it executes TurnOffCommand, which calls the TurnOff method on the Light object, resulting in the message "Light is turned off" being printed to the console.
Overall, the Command pattern is a powerful and useful pattern that can help you design more modular, maintainable, and extensible code, especially in situations where you need to isolate the invocation and execution of operations.
To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.
I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.
leave a comment