-
package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ /** * 创建命令接口 * * 把命令的调用者 * 命令的接收者 * 命令的执行者 * 进行解耦 */ public interface Command { //执行动作(操作) public void execute(); //撤销动作(操作) public void undo(); } ***************************************************************** package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class LightOffCommand implements Command{ //聚合LightReceiver LightReceiver light; //构造器 public LightOffCommand(LightReceiver light) { super(); this.light = light; } @Override public void execute() { //调用接收者的方法 light.off(); } @Override public void undo() { //调用接收者的方法 light.on(); } } ********************************************************************* package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class LightOnCommand implements Command{ //聚合LightReceiver LightReceiver light; //构造器 public LightOnCommand(LightReceiver light) { super(); this.light = light; } @Override public void execute() { //调用接收者的方法 light.on(); } @Override public void undo() { //调用接收者的方法 light.off(); } } ******************************************************************* package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class LightReceiver { public void on() { System.out.println("电灯打开了..."); } public void off() { System.out.println("电灯关闭了..."); } } ******************************************************************* package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ /** * 没有任何命令,即空执行:用于初始化每个按钮,当调用空命令,对象什么都不做 * 其实,这也是一种设计模式,可以省掉对空判断 */ public class NoCommand implements Command{ @Override public void execute() { } @Override public void undo() { } } ************************************************************************** package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class RemoteController { //开按钮的命令数组 Command[] onCommands; Command[] offCommands; //执行撤销的命令 Command undoCommand; //构造器,完成对按钮初始化 public RemoteController() { onCommands = new Command[5]; offCommands = new Command[5]; for (int i = 0; i < 5; i++) { onCommands[i] = new NoCommand(); offCommands[i] = new NoCommand(); } } //给我们的按钮设置你需要的命令即可 public void setOnCommand(int no, Command onCommand, Command offCommand) { onCommands[no] = onCommand; offCommands[no] = offCommand; } //按下开的按钮 public void onButtonWasPushed(int no) { //找到你按下的开的按钮,并调用对应的方法 onCommands[no].execute(); //记录这次的操作,用于撤销 undoCommand = onCommands[no]; } //按下关的按钮 public void offButtonWasPushed(int no) { //找到你按下的关的按钮,并调用对应的方法 offCommands[no].execute(); //记录这次的操作,用于撤销 undoCommand = offCommands[no]; } //按下撤销按钮 public void undoButtonWasPushed() { undoCommand.undo(); } } ********************************************************************* package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class TVOffCommand implements Command { //聚合LightReceiver TVReceiver tv; //构造器 public TVOffCommand(TVReceiver tv) { super(); this.tv = tv; } @Override public void execute() { //调用接收者的方法 tv.off(); } @Override public void undo() { //调用接收者的方法 tv.on(); } } ********************************************************************* package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class TVOnCommand implements Command { //聚合LightReceiver TVReceiver tv; //构造器 public TVOnCommand(TVReceiver tv) { super(); this.tv = tv; } @Override public void execute() { //调用接收者的方法 tv.on(); } @Override public void undo() { //调用接收者的方法 tv.off(); } } ********************************************************************* package com.atguigu.command; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class TVReceiver { public void on() { System.out.println("电视机打开了..."); } public void off() { System.out.println("电视机关闭了..."); } } ********************************************************************** public class Client { public static void main(String[] args) { //使用命令设计模式,完成通过遥控器,对电灯的操作 //创建电灯的对象(接受者) LightReceiver lightReceiver = new LightReceiver(); //创建电灯相关的开关命令 LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver); LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver); //需要一个遥控器 RemoteController remoteController = new RemoteController(); //给我们的遥控器设置相关的命令,比如no = 0 是电灯的开和关操作 remoteController.setOnCommand(0,lightOnCommand,lightOffCommand); System.out.println("--------------------按下灯的开的按钮------------------"); remoteController.onButtonWasPushed(0); System.out.println("--------------------按下灯的关的按钮------------------"); remoteController.offButtonWasPushed(0); System.out.println("--------------------撤销按钮-------------------------"); remoteController.undoButtonWasPushed(); System.out.println("==============使用遥控器操作电视机======================"); TVReceiver tvReceiver = new TVReceiver(); TVOffCommand tvoffCommand = new TVOffCommand(tvReceiver); TVOnCommand tvOnCommand = new TVOnCommand(tvReceiver); //给我们的遥控器设置相关的命令,比如no = 1 是电视机的开和关操作 remoteController.setOnCommand(1,tvOnCommand,tvoffCommand); System.out.println("--------------------按下电视机的开的按钮------------------"); remoteController.onButtonWasPushed(1); System.out.println("--------------------按下电视机的关的按钮------------------"); remoteController.offButtonWasPushed(1); System.out.println("--------------------撤销按钮-------------------------"); remoteController.undoButtonWasPushed(); } }
Java设计模式之命令模式
于 2022-09-04 21:14:54 首次发布