本文共 4959 字,大约阅读时间需要 16 分钟。
我们买了一套智能家电,有照明灯、风扇、冰箱、洗衣机,我们只要在手机上安装APP就可以控制对这些家电工作。
这些智能家电来自不同的厂家,我们不想针对每一种家电都安装一个APP,分别控制,我们希望只要一个APP就可以控制全部智能家电。 要实现一个APP控制所有智能家电的需要,则每个智能家电厂家都要提供一个统一的接口给APP调用,这时就可以考虑使用命令模式。 命令模式可将“动作的请求者”从“动作的执行者”对象中解耦出来。 在我们的例子中,动作的请求者是手机APP,动作的执行者是每个厂商的一个家电产品。
package com.lm.command;//创建命令接口public interface Command { //执行动作(操作) public void execute(); //撤销动作 public void undo();}
package com.lm.command;//public class LightReceiver { public void on() { System.out.println("电灯打开了"); } public void off() { System.out.println("电灯关闭了"); }}
package com.lm.command;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.lm.command;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.lm.command;//没有任何命令,即空执行:用于初始化每个按钮,当调用空命令时,对象什么都不做//其实,这样是一种设计模式,可以省掉对空判断public class NoCommand implements Command { @Override public void execute() { } @Override public void undo() { }}
package com.lm.command;//public class TVReceiver { public void on() { System.out.println("电视机打开了"); } public void off() { System.out.println("电视机关闭了"); }}
package com.lm.command;public class TVOnCommand implements Command { //聚合TVReceiver TVReceiver tv; public TVOnCommand(TVReceiver tv) { super(); this.tv = tv; } @Override public void execute() { //调用接收者的方法 tv.on(); } @Override public void undo() { //调用接收者的方法 tv.off(); }}
package com.lm.command;public class TVOffCommand implements Command { //聚合TVReceiver TVReceiver tv; public TVOffCommand(TVReceiver tv) { super(); this.tv = tv; } @Override public void execute() { tv.off(); } @Override public void undo() { tv.on(); }}
package com.lm.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(); TVOnCommand tvOnCommand = new TVOnCommand(tvReceiver); TVOffCommand tvOffCommand = new TVOffCommand(tvReceiver); remoteController.setCommand(1, tvOnCommand, tvOffCommand); System.out.println("----------按下电视机的开按钮----------"); remoteController.onButtonWasPushed(1); System.out.println("----------按下电视机的关按钮----------"); remoteController.offButtonWasPushed(1); System.out.println("----------按下电视机的撤销按钮----------"); remoteController.undoButtonWasPushed(); }}
转载地址:http://cugiz.baihongyu.com/