看一个具体的需求
package com.example.demo.command; /** * 创建命令接口 * @author zhaozhaohai * */ public interface Command { /** * 执行动作(操作) */ public void execute(); /** * 撤销动作(操作) */ public void undo(); } package com.example.demo.command; public class LightOffCommand implements Command{ /** * 聚合LightReceiver */ private LightReceiver LightReceiver; public LightOffCommand(LightReceiver lightReceiver) { this.LightReceiver = lightReceiver; } @Override public void execute() { // TODO Auto-generated method stub LightReceiver.off(); } @Override public void undo() { // TODO Auto-generated method stub LightReceiver.on(); } } package com.example.demo.command; public class LightOnCommand implements Command{ /** * 聚合LightReceiver */ private LightReceiver LightReceiver; public LightOnCommand(LightReceiver lightReceiver) { this.LightReceiver = lightReceiver; } @Override public void execute() { // TODO Auto-generated method stub // 调用接收者方法 LightReceiver.on(); } @Override public void undo() { // TODO Auto-generated method stub // 调用接收者方法 LightReceiver.off(); } } package com.example.demo.command; public class LightReceiver { public void on() { System.out.println(" 电灯打开了。。。 "); } public void off() { System.out.println(" 电灯关闭了。。。 "); } } package com.example.demo.command; /** * 没有任何命令,即空执行 :用于初始化每个按钮,当调用空命令时,对象什么都不做其实,这样是一种设计模式,可以省掉对空判断 * @author zhaozhaohai * */ public class NoCommand implements Command{ @Override public void execute() { // TODO Auto-generated method stub } @Override public void undo() { // TODO Auto-generated method stub } } package com.example.demo.command; public class RemoteController { // 开 按钮的命令数组 private Command[] onCommands; private Command[] offCommands; // 执行撤销的命令 private 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(); } } /** * 给我们的按钮设置你需要的命令 * @param no * @param onCommand * @param offCommand */ public void setCommand(int no, Command onCommand, Command offCommand) { onCommands[no] = onCommand; offCommands[no] = offCommand; } /** * 按下开按钮 * @param no */ public void onButtonWasPushed(int no) { // 找到你按下的开的按钮,并调用对应的方法 onCommands[no].execute(); // 记录这次的操作,用于撤销 undoCommand = onCommands[no]; } /** * 按下关的按钮 * @param no */ public void offButtonWasPushed(int no) { // 找到你按下的关的按钮,并调用对应方法 offCommands[no].execute(); // 记录这次的操作,用于撤销 undoCommand = offCommands[no]; } /** * 按下撤销按钮 */ public void undoButtonWasPushed() { undoCommand.undo(); } } package com.example.demo.command; public class TVOffCommand implements Command{ /** * 聚合TVReceiver */ private TVReceiver tvReceiver; public TVOffCommand(TVReceiver tvReceiver) { this.tvReceiver = tvReceiver; } @Override public void execute() { // TODO Auto-generated method stub tvReceiver.off(); } @Override public void undo() { // TODO Auto-generated method stub tvReceiver.on(); } } package com.example.demo.command; public class TVOnCommand implements Command{ /** * 聚合TVReceiver */ private TVReceiver tvReceiver; public TVOnCommand(TVReceiver tvReceiver) { this.tvReceiver = tvReceiver; } @Override public void execute() { // TODO Auto-generated method stub tvReceiver.on(); } @Override public void undo() { // TODO Auto-generated method stub tvReceiver.off(); } } package com.example.demo.command; public class TVReceiver { public void on() { System.out.println(" 电视机打开了。。。 "); } public void off() { System.out.println(" 电视机关闭了。。。 "); } } package com.example.demo.command; 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.setCommand(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.setCommand(1, tvOnCommand, tvOffCommand); System.out.println("----------按下开的按钮------------"); remoteController.onButtonWasPushed(1); System.out.println("----------按下关的按钮------------"); remoteController.offButtonWasPushed(1); System.out.println("----------按下撤销的按钮------------"); remoteController.undoButtonWasPushed(); } }
1)将发起请求的对象与执行请求的对象解耦。发起请求的对象是调用者,调用者只要命令对象的execute()方法就可以让接收者工作,而不必知道具体的接收者对象是谁、是如何实现的,命令对象会负责让接收者执行请求的动作,也就是说 :“请求发起者”和“请求执行者”之间的解耦是通过命令对象实现的,命令对象起到了纽带桥梁的作用。 |
|
来自: python_lover > 《待分类》