package F_Command.b;
/**
* 宏命令以及状态撤销
* CeilingFan:可撤销状态
*/
public class Main {
public static void main(String[] args) {
RemoteControl remote = new RemoteControl();
Light light = new Light();
Stereo stereo = new Stereo();
/**
* 创建电灯和音响的所有命令
*/
LightOnCommand lightOnCommand = new LightOnCommand(light);
LightOffCommand lightOffCommand = new LightOffCommand(light);
StereoOnWithCDCommand stereoOnWithCDCommand = new StereoOnWithCDCommand(
stereo);
StereoOffWithCDCommand stereoOffWithCDCommand = new StereoOffWithCDCommand(
stereo);
// 命令加载到遥控器插槽中
remote.setCommand(0, lightOnCommand, lightOffCommand);
remote.setCommand(1, stereoOnWithCDCommand, stereoOffWithCDCommand);
System.out.println(remote);
remote.onButtonWasPushed(0);
remote.offButtonWasPushed(0);
remote.undoButtonWasPushed();
remote.onButtonWasPushed(1);
remote.offButtonWasPushed(1);
System.out.println("===========");
// 使用宏命令
Command[] partyOn = { lightOnCommand, stereoOnWithCDCommand };
Command[] partyOff = { lightOffCommand, stereoOffWithCDCommand };
MacroCommand partOnMacro = new MacroCommand(partyOn);
MacroCommand partyOffMacro = new MacroCommand(partyOff);
remote.setCommand(2, partOnMacro, partyOffMacro);
remote.onButtonWasPushed(2);
remote.offButtonWasPushed(2);
remote.undoButtonWasPushed();
}
}
package F_Command.b;
public class NoCommand implements Command {
@Override
public void execute() {
}
@Override
public void undo() {
}
}
package F_Command.b;
import java.util.Arrays;
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
// 记录执行命令的前一个对象,方便撤销动作
Command undoCommand;
/**
* 初始化遥控器7个开与关的命令
*/
public RemoteControl() {
onCommands = new Command[7];
offCommands = new Command[7];
NoCommand 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) {
onCommands[slot] = onCommand;
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();
}
@Override
public String toString() {
return "RemoteControl [onCommands=" + Arrays.toString(onCommands)
+ ", offCommands=" + Arrays.toString(offCommands) + "]";
}
}
package F_Command.b;
public class Stereo {
public void on() {
System.out.println(“on”);
}
public void setCD() {
System.out.println("setCD");
}
public void off() {
System.out.println("off");
}
}
package F_Command.b;
/**
* 实现打开电灯的命令
*
*/
public class StereoOffWithCDCommand implements Command {
Stereo stereo;
public StereoOffWithCDCommand(Stereo stereo) {
this.stereo = stereo;
}
public void execute() {
stereo.off();
}
@Override
public void undo() {
stereo.on();
}
}
package F_Command.b;
/**
* 实现打开电灯的命令
*
*/
public class StereoOnWithCDCommand implements Command {
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo) {
this.stereo = stereo;
}
public void execute() {
stereo.on();
stereo.setCD();
}
@Override
public void undo() {
stereo.off();
}
}
package F_Command.b;
/**
* 使用宏命令执行一堆任务
*/
public class MacroCommand implements Command {
Command[] commands;
public MacroCommand(Command[] commands) {
this.commands = commands;
}
@Override
public void execute() {
for (int i = commands.length - 1; i >= 0; i--) {
commands[i].execute();
}
}
@Override
public void undo() {
for (int i = commands.length - 1; i >= 0; i--) {
commands[i].undo();
}
}
}
package F_Command.b;
/**
* 实现打开电灯的命令
*
*/
public class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.on();
}
@Override
public void undo() {
light.off();
}
}
package F_Command.b;
/**
* 实现打开电灯的命令
*
*/
public class LightOffCommand implements Command {
Light light;
public LightOffCommand(Light light) {
this.light = light;
}
public void execute() {
light.off();
}
@Override
public void undo() {
light.on();
}
}
package F_Command.b;
public class Light {
public void on() {
System.out.println(“on”);
}
public void off() {
System.out.println("off");
}
}
package F_Command.b;
/**
* 命令接口
*
*/
public interface Command {
void execute();
void undo();
}
package F_Command.b;
public class CeilingFanHighCommand implements Command {
CeilingFan ceilingFan;
// 方便追踪之前的状态
int prevSpeed;
public CeilingFanHighCommand(CeilingFan ceilingFan) {
this.ceilingFan = ceilingFan;
}
public void execute() {
prevSpeed = ceilingFan.getSpeed();
ceilingFan.high();
}
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 F_Command.b;
/**
* 吊扇类
* 使用状态进行撤销
*/
public class CeilingFan {
public static final int HIGH = 3;
public static final int MEDIUM = 2;
public static final int LOW = 1;
public static final int OFF = 0;
String location;
int speed;
public CeilingFan(String location) {
this.location = location;
speed = OFF;
}
public void high() {
speed = HIGH;
}
public void medium() {
speed = MEDIUM;
}
public void low() {
speed = LOW;
}
public void off() {
speed = OFF;
}
public int getSpeed() {
return speed;
}
}