类图如下:
创建Command接口:
/*命令接口*/public interface Command {/*执行命令*/void execute();/*撤销命令*/void undo();}
创建Command接口的实现类NoCommand,该不执行任何指令:
/*什么都不做命令*/public class NoCommand implements Command{@Overridepublic void execute() {}@Overridepublic void undo() {}}
创建电灯类Light,命令接收者,也就是LightOnCommand、LightOffCommand真正的执行者:
/*** 家具: 电灯* Receiver:命令接收者,也就是命令真正的执行者*/public class Light {/*执行命令*/public void on() {System.out.println("打开 电灯 ......");}/*撤销命令*/public void off() {System.out.println("关闭 电灯 ......");}}
创建Command接口的实现类LightOnCommand,控制电灯开操作:
/*电灯 开 命令*/public class LightOnCommand implements Command {private Light light;public LightOnCommand(Light light) {this.light = light;}@Overridepublic void execute() {light.on();}@Overridepublic void undo() {light.off();}}
创建Command接口的实现类LightOffCommand,控制电灯关操作:
/*电灯 关 命令*/public class LightOffCommand implements Command {private Light light;public LightOffCommand(Light light) {this.light = light;}@Overridepublic void execute() {light.off();}@Overridepublic void undo() {light.on();}}
创建电视类TV,命令接收者,也就是TVOnCommand、TVOffCommand真正的执行者:
/*** 家具: 电视* Receiver:命令接收者,也就是命令真正的执行者*/public class TV {/*执行命令*/public void on() {System.out.println("打开 电视 ......");}/*撤销命令*/public void off() {System.out.println("关闭 电视 ......");}}
创建Command接口的实现类TVOnCommand,控制电视开操作:
/*电视 开 命令*/public class TVOnCommand implements Command {private TV tv;public TVOnCommand(TV tv) {this.tv = tv;}@Overridepublic void execute() {tv.on();}@Overridepublic void undo() {tv.off();}}
创建Command接口的实现类TVOffCommand,控制电视关操作:
/*电视 关 命令*/public class TVOffCommand implements Command {private TV tv;public TVOffCommand(TV tv) {this.tv = tv;}@Overridepublic void execute() {tv.off();}@Overridepublic void undo() {tv.on();}}
创建遥控器类RemoteControl,通过它来调用命令,控制家用电器:
/*** 遥控器* Invoker:通过它来调用命令,*/public class RemoteControl {private int arrayLength = 5;/*遥控器上 开这一列 的命令*/private Command[] onCommandArray = new Command[arrayLength];/*遥控器上 关这一列 的命令*/private Command[] offCommandArray = new Command[arrayLength];/*遥控器上 撤销这一列 的命令*/private Command[] undoCommandArray = new Command[arrayLength];public RemoteControl() {for (int i = 0; i < arrayLength; i++) {onCommandArray[i] = new NoCommand();offCommandArray[i] = new NoCommand();undoCommandArray[i] = new NoCommand();}}public void setCommand(int index, Command onCommand, Command offCommand) {onCommandArray[index] = onCommand;offCommandArray[index] = offCommand;}public void onButtonPushed(int index) {onCommandArray[index].execute();undoCommandArray[index] = onCommandArray[index];}public void offButtonPushed(int index) {offCommandArray[index].execute();undoCommandArray[index] = offCommandArray[index];}public void undoButtonPushed(int index) {undoCommandArray[index].undo();}}
客户端:
/*客户端*/public class Client {public static void main(String[] args) {// 创建一个遥控器RemoteControl remoteControl = new RemoteControl();System.out.println("=================== 操作电灯相关指令 =======================");// 设置电灯的相关指令Light light = new Light();Command lightOnCommand = new LightOnCommand(light);Command lightOffCommand = new LightOffCommand(light);// 将电灯的命令,绑定到遥控器上remoteControl.setCommand(0, lightOnCommand, lightOffCommand);//操作 电灯System.out.println("------- 打开 ---------");remoteControl.onButtonPushed(0);System.out.println("------- 关闭 ---------");remoteControl.offButtonPushed(0);System.out.println("------- 撤销 ---------");remoteControl.undoButtonPushed(0);System.out.println("=================== 操作电视相关指令 =======================");// 设置电视的相关指令TV tv = new TV();Command tvOnCommand = new TVOnCommand(tv);Command tvOffCommand = new TVOffCommand(tv);// 将电视的命令,绑定到遥控器上remoteControl.setCommand(1, tvOnCommand, tvOffCommand);//操作 电视System.out.println("------- 打开 ---------");remoteControl.onButtonPushed(1);System.out.println("------- 关闭 ---------");remoteControl.offButtonPushed(1);System.out.println("------- 撤销 ---------");remoteControl.undoButtonPushed(1);}}
输出如下:
=================== 操作电灯相关指令 =======================------- 打开 ---------打开 电灯 ......------- 关闭 ---------关闭 电灯 ......------- 撤销 ---------打开 电灯 ......=================== 操作电视相关指令 =======================------- 打开 ---------打开 电视 ......------- 关闭 ---------关闭 电视 ......------- 撤销 ---------打开 电视 ......
