The Command Design Pattern is a behavioral design pattern that turns a request into a stand-alone object. This object contains all the information about the request, including the action to be taken and any parameters. This pattern allows you to parameterize objects with different operations, delay or queue a request, and support undoable operations.
Here’s a C# example of the Command Design Pattern with a use case for controlling home automation:
Create the Command Interface
Define an interface for all concrete command classes. This interface should include an Execute
method.
public interface ICommand
{
void Execute();
}
Create Concrete Command Classes
Implement the command interface with concrete command classes that encapsulate specific actions.
public class LightOnCommand : ICommand
{
private Light light;
public LightOnCommand(Light light)
{
this.light = light;
}
public void Execute()
{
light.TurnOn();
}
}
public class LightOffCommand : ICommand
{
private Light light;
public LightOffCommand(Light light)
{
this.light = light;
}
public void Execute()
{
light.TurnOff();
}
}
public class GarageDoorOpenCommand : ICommand
{
private GarageDoor garageDoor;
public GarageDoorOpenCommand(GarageDoor garageDoor)
{
this.garageDoor = garageDoor;
}
public void Execute()
{
garageDoor.Open();
}
}
// Add more command classes for other devices or actions.
Create the Receiver Classes
Define classes that will receive the commands. These classes contain the methods that perform the actual actions.
public class Light
{
public void TurnOn()
{
Console.WriteLine("Light is on");
}
public void TurnOff()
{
Console.WriteLine("Light is off");
}
}
public class GarageDoor
{
public void Open()
{
Console.WriteLine("Garage door is open");
}
}
// Add more receiver classes for other devices.
Create the Invoker Class
Create an invoker class that will be responsible for executing commands.
public class RemoteControl
{
private ICommand command;
public void SetCommand(ICommand command)
{
this.command = command;
}
public void PressButton()
{
command.Execute();
}
}
Use Case – Home Automation Control
Use the command pattern to control home automation devices using the remote control.
class Program
{
static void Main(string[] args)
{
Light livingRoomLight = new Light();
GarageDoor garageDoor = new GarageDoor();
ICommand lightOn = new LightOnCommand(livingRoomLight);
ICommand lightOff = new LightOffCommand(livingRoomLight);
ICommand garageOpen = new GarageDoorOpenCommand(garageDoor);
RemoteControl remote = new RemoteControl();
remote.SetCommand(lightOn);
remote.PressButton(); // Turns on the light
remote.SetCommand(lightOff);
remote.PressButton(); // Turns off the light
remote.SetCommand(garageOpen);
remote.PressButton(); // Opens the garage door
}
}
In this example, the Command Design Pattern decouples the sender (the RemoteControl
) from the receiver (the devices) and encapsulates the request as a command object. This allows you to change the behavior of the RemoteControl
by simply swapping different command objects, making it easy to extend and maintain the system.