using System;
// Command interface
public interface ICommand
{
void Execute();
}
// Concrete command
public class LightOnCommand : ICommand
{
private Light _light;
public LightOnCommand(Light light)
{
_light = light;
}
public void Execute()
{
_light.TurnOn();
}
}
// Receiver
public class Light
{
public void TurnOn()
{
Console.WriteLine("Light is turned on");
}
public void TurnOff()
{
Console.WriteLine("Light is turned off");
}
}
// Invoker
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)
{
// Client code
Light livingRoomLight = new Light();
ICommand lightOnCommand = new LightOnCommand(livingRoomLight);
RemoteControl remote = new RemoteControl();
remote.SetCommand(lightOnCommand);
// Simulate pressing the button
remote.PressButton();
}
}