设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释
生活随笔
收集整理的這篇文章主要介紹了
设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
命令模式(command pattern) 撤銷(undo) 詳細解釋
本文地址:?http://blog.csdn.net/caroline_wendy
參考命令模式:?http://blog.csdn.net/caroline_wendy/article/details/31379977
命令模式能夠用于運行撤銷(undo)操作.
詳細方法:
1. 對象類中須要保存狀態, 如level.
package command;public class CeilingFan {String location = "";int level;public static final int HIGH = 3;public static final int MEDIUM = 2;public static final int LOW = 1;public static final int OFF = 0;public CeilingFan(String location) {this.location = location;}public void high() {// turns the ceiling fan on to highlevel = HIGH;System.out.println(location + " ceiling fan is on high");} public void medium() {// turns the ceiling fan on to mediumlevel = MEDIUM;System.out.println(location + " ceiling fan is on medium");}public void low() {// turns the ceiling fan on to lowlevel = LOW;System.out.println(location + " ceiling fan is on low");}public void off() {// turns the ceiling fan offlevel = OFF;System.out.println(location + " ceiling fan is off");}public int getSpeed() {return level;} }2. 命令接口, 包括撤銷(undo)操作, 即依據傳入對象的狀態參數, 推斷詳細的撤銷動作./*** @time 2014年6月9日*/ package command;/*** @author C.L.Wang**/ public interface Command {public void execute();public void undo(); //撤銷 }
3. 詳細命令(Concrete Command)類須要實現, 撤銷操作, 依據不同狀態, 運行不同的撤銷操作./*** @time 2014年6月16日*/ package command;/*** @author C.L.Wang**/ public class CeilingFanHighCommand implements Command {CeilingFan ceilingFan;int prevSpeed;public CeilingFanHighCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}/* (non-Javadoc)* @see command.Command#execute()*/@Overridepublic void execute() {// TODO Auto-generated method stubprevSpeed = ceilingFan.getSpeed();ceilingFan.high();}/* (non-Javadoc)* @see command.Command#undo()*/@Overridepublic void undo() {// TODO Auto-generated method stubif (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}}}package command;public class CeilingFanMediumCommand implements Command {CeilingFan ceilingFan;int prevSpeed;public CeilingFanMediumCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}public void execute() {prevSpeed = ceilingFan.getSpeed();ceilingFan.medium();}public void undo() {if (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}} }package command;public class CeilingFanLowCommand implements Command {CeilingFan ceilingFan;int prevSpeed;public CeilingFanLowCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}public void execute() {prevSpeed = ceilingFan.getSpeed();ceilingFan.low();}public void undo() {if (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}} }package command;public class CeilingFanOffCommand implements Command {CeilingFan ceilingFan;int prevSpeed;public CeilingFanOffCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}public void execute() {prevSpeed = ceilingFan.getSpeed();ceilingFan.off();}public void undo() {if (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}} }
4. 接受者(Receiver)類實現撤銷(undo)操作, 即在調用命令時, 保留命令, 運行撤銷(undo)操作時, 調用保留的命令./*** @time 2014年6月16日*/ package command;/*** @author C.L.Wang**/ public class RemoteControl {Command[] onCommands; //開Command[] offCommands; //關Command undoCommand; //撤銷public RemoteControl() {onCommands = new Command[7];offCommands = new Command[7];Command noCommand = new NoCommand();for (int i=0; i<7; ++i) { //初始化onCommands[i] = noCommand;offCommands[i] = noCommand;}undoCommand = noCommand;}public void setCommand (int slot, Command onCommand, Command offCommand) {this.onCommands[slot] = onCommand;this.offCommands[slot] = offCommand;}public void onButtonWasPushed(int slot) { //開啟buttononCommands[slot].execute();undoCommand = onCommands[slot];}public void offButtonWasPushed(int slot) { //關閉buttonoffCommands[slot].execute();undoCommand = offCommands[slot];}public void undoButtonWasPushed() {undoCommand.undo();}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();} }
5. 測試類, 調用不同的命令, 保存不同的狀態, 運行撤銷操作.
/*** @time 2014年6月16日*/ package command;import javax.crypto.spec.IvParameterSpec;/*** @author C.L.Wang**/ public class RemoteLoader {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubRemoteControl remoteControl = new RemoteControl();Light livingRoomLight = new Light("Living Room");Light kitchenLight = new Light("Kitchen");CeilingFan ceilingFan = new CeilingFan("Living Room");GarageDoor garageDoor = new GarageDoor("");Stereo stereo = new Stereo("Living Room");LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);GarageDoorOnCommand garageDoorOn = new GarageDoorOnCommand(garageDoor);GarageDoorOffCommand garageDoorOff = new GarageDoorOffCommand(garageDoor);StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo);remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); //設這遙控器remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);remoteControl.setCommand(2, ceilingFanHigh, ceilingFanOff);remoteControl.setCommand(3, ceilingFanMedium, ceilingFanOff);remoteControl.setCommand(4, stereoOnWithCD, stereoOffCommand);remoteControl.onButtonWasPushed(2); //快速remoteControl.offButtonWasPushed(2); //關閉快速System.out.println(remoteControl);remoteControl.undoButtonWasPushed(); //退回快速System.out.println();remoteControl.onButtonWasPushed(3); //中速System.out.println(remoteControl);remoteControl.undoButtonWasPushed(); //快速}}6. 輸出:
Living Room ceiling fan is on high Living Room ceiling fan is off------ Remote Control ------ [slot 0] command.LightOnCommand command.LightOffCommand [slot 1] command.LightOnCommand command.LightOffCommand [slot 2] command.CeilingFanHighCommand command.CeilingFanOffCommand [slot 3] command.CeilingFanMediumCommand command.CeilingFanOffCommand [slot 4] command.StereoOnWithCDCommand command.StereoOffCommand [slot 5] command.NoCommand command.NoCommand [slot 6] command.NoCommand command.NoCommandLiving Room ceiling fan is on highLiving Room ceiling fan is on medium------ Remote Control ------ [slot 0] command.LightOnCommand command.LightOffCommand [slot 1] command.LightOnCommand command.LightOffCommand [slot 2] command.CeilingFanHighCommand command.CeilingFanOffCommand [slot 3] command.CeilingFanMediumCommand command.CeilingFanOffCommand [slot 4] command.StereoOnWithCDCommand command.StereoOffCommand [slot 5] command.NoCommand command.NoCommand [slot 6] command.NoCommand command.NoCommandLiving Room ceiling fan is on high其余代碼下載:?http://download.csdn.net/detail/u012515223/7507147
總結
以上是生活随笔為你收集整理的设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 新桌面安装器曝光:优化自动
- 下一篇: asp.net ajax控件工具集 Au