一、命令模式
将“请求”封装成对象,以便于使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。
二、举例
请设计一个遥控器,要求如下:
- 可以控制灯的开关
- 也可以扩展其他的命令控制
命令接口类
package com.jackeys.CommandMode;
/**
* @Description: 命令接口
* @ClassName: Command
* @Package com.jackeys.CommandMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/18 19:49
* @Version: 1.0
*/
public interface Command {
void execute();
}
电灯类
package com.jackeys.CommandMode;
/**
* @Description: 灯类
* @ClassName: Light
* @Package com.jackeys.CommandMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/18 20:01
* @Version: 1.0
*/
public class Light {
private String name;
public Light(String name) {
this.name = name;
}
/**
* @MethodName: off
* @Description: 关灯
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/18 20:01
* @Version: 1.0
*/
public void off() {
System.out.println(this.name + "的灯关闭了");
}
/**
* @MethodName: on
* @Description: 开灯
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/18 20:08
* @Version: 1.0
*/
public void on() {
System.out.println(this.name + "的灯打开了");
}
}
关灯命令类
package com.jackeys.CommandMode;
/**
* @Description: 关灯命令
* @ClassName: LightOffCommand
* @Package com.jackeys.CommandMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/18 20:02
* @Version: 1.0
*/
public class LightOffCommand implements Command {
private Light light;
public LightOffCommand(Light light) {
this.light = light;
}
/**
* @MethodName: execute
* @Description: 执行命令
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/18 20:03
* @Version: 1.0
*/
@Override
public void execute() {
this.light.off();
}
}
开灯命令类
package com.jackeys.CommandMode;
/**
* @Description: 开灯命令
* @ClassName: LightOnCommand
* @Package com.jackeys.CommandMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/18 20:13
* @Version: 1.0
*/
public class LightOnCommand implements Command {
private Light light;
public LightOnCommand(Light light) {
this.light = light;
}
/**
* @MethodName: execute
* @Description: 执行命令
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/18 20:14
* @Version: 1.0
*/
@Override
public void execute() {
this.light.on();
}
}
无命令类
package com.jackeys.CommandMode;
/**
* @Description: 没有命令
* @ClassName: NoCommand
* @Package com.jackeys.CommandMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/18 19:50
* @Version: 1.0
*/
public class NoCommand implements Command{
@Override
public void execute() {}
}
遥控器类
package com.jackeys.CommandMode;
/**
* @Description: 远程控制类
* @ClassName: RemoteControl
* @Package com.jackeys.CommandMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/18 19:51
* @Version: 1.0
*/
public class RemoteControl {
/**
* 开命令
*/
private Command[] onCommand;
/**
* 关命令
*/
private Command[] offCommand;
public RemoteControl() {
this.onCommand = new Command[7];
this.offCommand = new Command[7];
Command noCommand = new NoCommand();
for (int i = 0; i < 7; i++){
this.onCommand[i] = noCommand;
this.offCommand[i] = noCommand;
}
}
/**
* @MethodName: setCommand
* @Description: 放入命令
* @Param slot
* @Param onCommand
* @Param offCommand
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/18 19:54
* @Version: 1.0
*/
public void setCommand(int slot, Command onCommand, Command offCommand){
this.onCommand[slot] = onCommand;
this.offCommand[slot] = offCommand;
}
/**
* @MethodName: onButtonWasPushed
* @Description: 执行打开按钮的命令
* @Param slot
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/18 19:56
* @Version: 1.0
*/
public void onButtonWasPushed(int slot) {
this.onCommand[slot].execute();
}
/**
* @MethodName: offButtonWasPushed
* @Description: 执行关闭按钮的命令
* @Param slot
* @Return void
* @Auther: Jackeys 1623427947@qq.com
* @Date: 2021/10/18 19:56
* @Version: 1.0
*/
public void offButtonWasPushed(int slot){
this.offCommand[slot].execute();
}
@Override
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("\n------ Remote Control ------\n");
for (int i = 0; i < this.onCommand.length; i++){
stringBuffer.append("[slot " + i + "] " + onCommand[i].getClass().getName() + " " + this.offCommand.getClass().getName() + "\n");
}
return stringBuffer.toString();
}
}
测试
package com.jackeys.CommandMode;
/**
* @Description: 测试命令模式
* @ClassName: Test
* @Package com.jackeys.CommandMode
* @Author: Jackeys 1623427047@qq.com
* @Copyright 版权归Jackeys企业(或个人)所有
* @CreateTime: 2021/10/18 20:15
* @Version: 1.0
*/
public class Test {
public static void main(String[] args) {
RemoteControl remoteControl = new RemoteControl();
Light kitchenLight = new Light("厨房的灯");
Light doorLight = new Light("门口的灯");
Light roomLight = new Light("卧室的灯");
LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);
LightOffCommand kitchenLightOff =new LightOffCommand(kitchenLight);
LightOnCommand doorLightOn = new LightOnCommand(doorLight);
LightOffCommand doorLightOff = new LightOffCommand(doorLight);
LightOnCommand roomLightOn = new LightOnCommand(roomLight);
LightOffCommand roomLightOff = new LightOffCommand(roomLight);
remoteControl.setCommand(0,kitchenLightOn,kitchenLightOff);
remoteControl.setCommand(1,doorLightOn,doorLightOff);
remoteControl.setCommand(2,roomLightOn,roomLightOff);
System.out.println(remoteControl.toString());
remoteControl.onButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(0);
remoteControl.offButtonWasPushed(1);
remoteControl.offButtonWasPushed(2);
}
}
运行截图