1,概念
命令模式(Command Pattern):
将请求封装成对象,着可以让你使用不同的请求、队列,或者日志请求来参数化其他对象。命令模式也可以支持撤销操作。
2,场景
①需要将发出请求的对象和执行请求的对象解耦的时候,使用命令模式。
②系统需要在不同的时间指定请求、将请求排队和执行请求。
③系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作。
④系统需要将一组操作组合在一起,即支持宏命令。
3,优缺点
优:
①将“动作的请求者”从“动作的执行者”对象中解耦。
②新的命令可以很容易地加入到系统中。
③可以比较容易地设计一个组合命令。
④调用同一方法实现不同的功能
缺:
使用命令模式可能会导致某些系统有过多的具体命令类
4,实现
封装调用。
5,demo
package CommandPattern;
/**
* 实现遥控器
* @author luo
*
*/
public class Main {
public static final void main(String[] args){
Invoker_RemoteControlWithUndo remoteControl = new Invoker_RemoteControlWithUndo();
Receiver_Light livingRoomLight = new Receiver_Light("Living Room");
ConcreateComand_LightOn livingRoomLightOn = new ConcreateComand_LightOn(livingRoomLight);
ConcreateComand_LightOff livingRoomLightOff = new ConcreateComand_LightOff(livingRoomLight);
//将灯开关命令设置到0号插槽
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
/**
* 打开灯、关灯,然后撤销
*/
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.undoButtonWasPushed();
/**
* 关灯、开灯,撤销
*/
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(0);
remoteControl.undoButtonWasPushed();
}
}
package CommandPattern;
/**
* 为所有命令声明一个接口
* @author luo
*
*/
public interface Command {
/**
* 调用execute()方法,可以让接收者进行相关的动作。
*/
public void execute();
/**
* 调用undo()方法,撤销相关操作
*/
public void undo();
}
package CommandPattern;
public class ConcreateComand_LightOff implements Command{
private Receiver_Light light = null;
public ConcreateComand_LightOff(Receiver_Light light){
this.light = light;
}
@Override
public void execute() {
light.off();
}
@Override
public void undo() {
light.on();
}
}
package CommandPattern;
/**
* 具体命令类,实现Command接口。
* @author luo
*
*/
public class ConcreateComand_LightOn implements Command {
Receiver_Light light = null;
public ConcreateComand_LightOn(Receiver_Light light){
this.light = light;
}
@Override
public void execute() {
light.on();
}
@Override
public void undo() {
light.off();
}
}
package CommandPattern;
public class Receiver_Light {
public Receiver_Light(String lightName){
System.out.println("Receiver_Light" + lightName);
}
public void on(){
System.out.println("Receiver_Light:on");
}
public void off(){
System.out.println("Receiver_Light:off");
}
}
package CommandPattern;
/**
* 撤销按钮类
* 用来追踪最后被调用的命令
* @author luo
*
*/
public class Invoker_RemoteControlWithUndo {
Command[] onCommands;
Command[] offCommands;
/**纪律前一个命令*/
Command undoCommand;
public Invoker_RemoteControlWithUndo(){
//仅保存7条命令
onCommands = new Command[7];
offCommands = new Command[7];
Command noCommand = null;
for (int i=0; i<7; i++){
//初始化
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
undoCommand = null;
}
public void setCommand(int i, Command onCommand, Command offCommand){
onCommands[i] = onCommand;
offCommands[i] = offCommand;
}
public void onButtonWasPushed(int i){
onCommands[i].execute();
undoCommand = onCommands[i];
}
public void offButtonWasPushed(int i){
offCommands[i].execute();
undoCommand = offCommands[i];
}
public void undoButtonWasPushed(){
undoCommand.undo();
}
}