デザインモード-コマンドモード(command pattern)キャンセル(undo)詳細
コマンドモード(command pattern)キャンセル(undo)詳細
この記事のアドレス: http://blog.csdn.net/caroline_wendy
参照コマンドモード: http://blog.csdn.net/caroline_wendy/articale/detail/31379977
コマンドモードは、キャンセル操作を実行するために使用できます。
具体的な方法:
1.対象クラスにはlevelのような状態が必要です。
5.試験クラスは、異なるコマンドを呼び出し、異なる状態を保存し、取消操作を実行します。
6.出力:
残りのコードのダウンロード: http://download.csdn.net/detail/u012515223/7507147
この記事のアドレス: http://blog.csdn.net/caroline_wendy
参照コマンドモード: http://blog.csdn.net/caroline_wendy/articale/detail/31379977
コマンドモードは、キャンセル操作を実行するために使用できます。
具体的な方法:
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 high
level = HIGH;
System.out.println(location + " ceiling fan is on high");
}
public void medium() {
// turns the ceiling fan on to medium
level = MEDIUM;
System.out.println(location + " ceiling fan is on medium");
}
public void low() {
// turns the ceiling fan on to low
level = LOW;
System.out.println(location + " ceiling fan is on low");
}
public void off() {
// turns the ceiling fan off
level = 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()
*/
@Override
public void execute() {
// TODO Auto-generated method stub
prevSpeed = ceilingFan.getSpeed();
ceilingFan.high();
}
/* (non-Javadoc)
* @see command.Command#undo()
*/
@Override
public void undo() {
// TODO Auto-generated method stub
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 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)類は、キャンセル操作を実現します。すなわち、コマンドを呼び出した時に、保留コマンドを保留し、キャンセル操作を実行した時に、保留コマンドを呼び出します。/**
* @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) { //
onCommands[slot].execute();
undoCommand = onCommands[slot];
}
public void offButtonWasPushed(int slot) { //
offCommands[slot].execute();
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.試験クラスは、異なるコマンドを呼び出し、異なる状態を保存し、取消操作を実行します。
/**
* @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 stub
RemoteControl 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.NoCommand
Living Room ceiling fan is on high
Living 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.NoCommand
Living Room ceiling fan is on high
残りのコードのダウンロード: http://download.csdn.net/detail/u012515223/7507147