Command pattern undo
Command modeCan be used for executionUndo operation.
Specific Method:
1. required in the object classSave status, Such as 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. The command interface contains the undo operation, that is, the specific undo action is determined based on the input object status parameters.
/*** @ Time June 9, 2014 */package command;/*** @ author C. l. wang **/public interface Command {public void execute (); public void undo (); // undo}
3.
Specific Command (Concrete Command)Class needs to be implemented, cancel the operation, according
Different statusesTo perform different revocation operations.
/*** @ Time June 16, 2014 */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 classImplementation
Undo)Operation, that is, when a command is called,
Reserved command, Execute
Undo operation, Call
Reserved commands.
/*** @ Time June 16, 2014 */package command;/*** @ author C. l. wang **/public class RemoteControl {Command [] onCommands; // enable Command [] offCommands; // disable Command undoCommand; // cancel public RemoteControl () {onCommands = new Command [7]; offCommands = new Command [7]; Command noCommand = new NoCommand (); for (int I = 0; I <7; ++ I) {// initialize 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) {// reset cute (); undoCommand = onCommands [slot];} public void offButtonWasPushed (int slot) {// close the offcommands[slot0000.exe cute (); undoCommand = offCommands [slot];} public void undoButtonWasPushed () {undoCommand. undo ();} public String toString () {StringBuffer stringBuffer = new StringBuffer (); stringBuffer. append (------ Remote Control ------); for (int I = 0; I
5.Test class, Call different commands, save different statuses, and perform the Undo operation.
/*** @ Time June 16, 2014 */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 success = new LightOffCommand (livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand (kitchenLight); LightOffCommand success = new LightOffCommand (kitchenLight); Response success = new response (ceilingFan ); response parameters = new response (ceilingFan); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand (ceilingFan); Response garageDoorOn = new GarageDoorOnCommand (garageDoor); Response parameters = new response (garageDoor ); stereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand (stereo); StereoOffCommand stereoOffCommand = new StereoOffCommand (stereo); remoteControl. setCommand (0, livingRoomLightOn, livingRoomLightOff); // set this remote control remoteControl. setCommand (1, kitchenLightOn, kitchenLightOff); remoteControl. setCommand (2, ceilingFanHigh, ceilingFanOff); remoteControl. setCommand (3, ceilingFanMedium, ceilingFanOff); remoteControl. setCommand (4, stereoOnWithCD, stereoOffCommand); remoteControl. onButtonWasPushed (2); // high-speed remoteControl. offButtonWasPushed (2); // disable high-speed System. out. println (remoteControl); remoteControl. undoButtonWasPushed (); // return to high-speed System. out. println (); remoteControl. onButtonWasPushed (3); // Medium Speed System. out. println (remoteControl); remoteControl. undoButtonWasPushed (); // High Speed }}
6. Output:
Living Room ceiling fan is on highLiving 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
Other code downloads: http://download.csdn.net/detail/u012515223/7507147