设计模式——命令模式
作者:互联网
这次来看下命令模式:
先看下head first中对命令模式的定义:将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。
接下来,再来看下类图:
最后来看下代码:
public interface Command { void execute(); void undo(); }
public class LightOffCommand implements Command { private Light light = new Light(); public LightOffCommand() { this.light = light; } @Override public void execute() { light.off(); } @Override public void undo() { light.on(); } }
public class LightOnCommand implements Command { private Light light = new Light(); public LightOnCommand() { this.light = light; } @Override public void execute() { light.on(); } @Override public void undo() { light.off(); } }
public class Light { public void on() { System.out.println("Light is on"); } public void off() { System.out.println("Light is off"); } }
public class NoCommand implements Command { @Override public void execute() { } @Override public void undo() { } }
public class StereoOffWithCDCommand implements Command { private Stereo stereo = new Stereo(); public StereoOffWithCDCommand() { this.stereo = stereo; } @Override public void execute() { stereo.off(); } @Override public void undo() { stereo.on(); stereo.setCD(); stereo.setVolume(11); } }
public class StereoOnWithCDCommand implements Command { private Stereo stereo = new Stereo(); public StereoOnWithCDCommand() { this.stereo = stereo; } @Override public void execute() { stereo.on(); stereo.setCD(); stereo.setVolume(11); } @Override public void undo() { stereo.off(); } }
public class Stereo { public void on() { System.out.println("Stereo is on"); } public void setCD() { System.out.println("CD is set"); } public void setVolume(int volume) { System.out.println("Volume is set " + volume); } public void off() { System.out.println("Stereo is off"); } }
public class RemoteControl { private Command[] onCommands; private Command[] offCommands; private Command undoCommand; public RemoteControl() { onCommands = new Command[2]; offCommands = new Command[2]; Command command = new NoCommand(); for (int i = 0; i < 2; i++) { onCommands[i] = command; offCommands[i] = command; } undoCommand = command; } public void setCommand(int slot, Command onCommand, Command offCommand) { onCommands[slot] = onCommand; offCommands[slot] = offCommand; } public void onButtonWasPush(int slot) { onCommands[slot].execute(); undoCommand = onCommands[slot]; } public void offButtonWasPush(int slot) { offCommands[slot].execute(); undoCommand = offCommands[slot]; } public void undoButtonWasPushed() { undoCommand.undo(); } @Override public String toString() { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("\n------ Remote Control ------\n"); for (int i = 0; i < onCommands.length; i++) { stringBuffer.append("[slot " + i + "] " + onCommands[i].getClass().getName() + " " + offCommands[i].getClass().getName() + "\n"); } return stringBuffer.toString(); } }
public class RemoteControlTest { public static void main(String[] args) { RemoteControl remte = new RemoteControl(); LightOnCommand lightOnCommand = new LightOnCommand(); LightOffCommand lightOffCommand = new LightOffCommand(); StereoOnWithCDCommand onWithCDCommand = new StereoOnWithCDCommand(); StereoOffWithCDCommand offWithCDCommand = new StereoOffWithCDCommand(); remte.setCommand(0, lightOnCommand, lightOffCommand); remte.setCommand(1, onWithCDCommand, offWithCDCommand); System.out.println(remte.toString()); remte.onButtonWasPush(0); remte.onButtonWasPush(1); remte.offButtonWasPush(0); remte.offButtonWasPush(1); remte.undoButtonWasPushed(); } }
标签:stereo,void,模式,命令,Command,Override,new,设计模式,public 来源: https://www.cnblogs.com/shenqiaqia/p/11028496.html