命令模式(Command Pattern)是一种行为型设计模式,它将一个请求封装为一个对象,使发出请求的对象和执行请求的对象解耦。这样可以方便地对请求排队、记录日志、撤销/重做操作以及支持可扩展性。
// 命令接口
public interface Command {
void execute();
}
// 具体命令类 - 开灯命令
public class TurnOnLightCommand implements Command {
private final Light light;
public TurnOnLightCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.turnOn();
}
}
// 接收者 - 灯光控制类
public class Light {
public void turnOn() {
System.out.println("The light is on.");
}
public void turnOff() {
System.out.println("The light is off.");
}
}
// 调用者 - 控制器类
public class RemoteControl {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void pressButton() {
if (command != null) {
command.execute();
} else {
System.out.println("No command assigned to the remote control.");
}
}
}
// 使用示例
public class Client {
public static void main(String[] args) {
Light light = new Light();
Command turnOnCommand = new TurnOnLightCommand(light);
RemoteControl remote = new RemoteControl();
remote.setCommand(turnOnCommand);
remote.pressButton(); // 输出: The light is on.
}
}
想象你正在使用遥控器操作电视。遥控器是调用者,开关电视的操作就是命令,而电视则是接收者。当你按下遥控器上的开关键时,实际上是向电视发送了一个“打开”的命令,电视接收到这个命令后执行打开操作。这种情况下,无论遥控器如何变化(比如换成了语音遥控器),只要遵循相同的命令接口,都能控制电视打开或关闭。