命令模式第一篇中把问题简化成一个遥控器,它只有一个按钮和对应的插槽,可以控制一个装置。
根据需求,现在有7个插槽,每个插槽都有一个开和关按钮,可以控制一个装置。只需将远程控制
对象中的单个命令组件换成命令组件数组即可。再加一个控制整个装置的撤销按钮。
实现代码如下:
public interface Command {
public void execute();
public void undo();
}
public class GargeDoorCloseCommand implements Command{
GargeDoor gargeDoor;
public GargeDoorCloseCommand(GargeDoor gargeDoor)
{
this.gargeDoor=gargeDoor;
}
@Override
public void execute() {
// TODO Auto-generated method stub
gargeDoor.close();
}
@Override
public void undo() {
// TODO Auto-generated method stub
gargeDoor.open();
}
}
public class GargeDoorOpenCommand implements Command{
GargeDoor gargeDoor;
public GargeDoorOpenCommand(GargeDoor gargeDoor)
{
this.gargeDoor=gargeDoor;
}
@Override
public void execute() {
// TODO Auto-generated method stub
gargeDoor.open();
}
@Override
public void undo() {
// TODO Auto-generated method stub
gargeDoor.close();
}
}
public class LightOffCommand implements Command{
Light light;
public LightOffCommand(Light light)
{
this.light=light;
}
@Override
public void execute() {
// TODO Auto-generated method stub
light.off();
}
@Override
public void undo() {
// TODO Auto-generated method stub
light.on();
}
}
public class LightOnCommand implements Command{
Light light;
public LightOnCommand(Light light)
{
this.light=light;
}
@Override
public void execute() {
// TODO Auto-generated method stub
light.on();
}
@Override
public void undo() {
// TODO Auto-generated method stub
light.off();
}
}
public class NoCommand implements Command{
@Override
public void execute() {
// TODO Auto-generated method stub
}
@Override
public void undo() {
// TODO Auto-generated method stub
}
}
public class StereoOffCommand implements Command{
Stereo stereo;
public StereoOffCommand(Stereo stereo)
{
this.stereo=stereo;
}
@Override
public void execute() {
// TODO Auto-generated method stub
stereo.off();
}
@Override
public void undo() {
// TODO Auto-generated method stub
stereo.on();
}
}
public class StereoOnWithCDCommand implements Command{
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo)
{
this.stereo=stereo;
}
@Override
public void execute() {
// TODO Auto-generated method stub
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
@Override
public void undo() {
// TODO Auto-generated method stub
stereo.off();
}
}
public class Light {
String description;
public Light(String description)
{
this.description=description;
}
public void on()
{
System.out.println(description+"light is on");
}
public void off()
{
System.out.println(description+"light is close");
}
}
public class GargeDoor {
String description;
public GargeDoor(String description)
{
this.description=description;
}
public void open()
{
System.out.println(description+"door is open");
}
public void close()
{
System.out.println(description+"door is close");
}
}
public class Stereo {
int volume;
String description;
public Stereo(String description)
{
this.description=description;
}
public void on()
{
System.out.println(description+"stereo on");
}
public void setCD()
{
}
public void setVolume(int volume)
{
this.volume=volume;
}
public void off()
{
System.out.println(description+"stereo off");
}
}
public class SimpleRemoteControl {
Command[] onCommands;
Command[] offCommands;
Command undoCommand;
static final int number=7;
public SimpleRemoteControl()
{
onCommands=new Command[7];
offCommands=new Command[7];
Command noCommand=new NoCommand();
for(int i=0;i<number;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 onButtonWasPressed(int slot)
{
onCommands[slot].execute();
undoCommand=onCommands[slot];
}
public void offButtonWasPressed(int slot)
{
offCommands[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();
}
}
public class RemoteControlTest {
public static void main(String[] args)
{
SimpleRemoteControl remoteControl=new SimpleRemoteControl();
Light livingRoomLight=new Light("Living Room");
Light kitchenLight=new Light("KitChen");
GargeDoor gargeDoor=new GargeDoor("");
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);
GargeDoorOpenCommand gargeDooOpenr=new GargeDoorOpenCommand(gargeDoor);
GargeDoorCloseCommand gargeDooClose=new GargeDoorCloseCommand(gargeDoor);
StereoOnWithCDCommand stereoOnWithCD=new StereoOnWithCDCommand(stereo);
StereoOffCommand stereoOff=new StereoOffCommand(stereo);
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl.setCommand(2, gargeDooOpenr, gargeDooClose);
remoteControl.setCommand(3, stereoOnWithCD, stereoOff);
System.out.println(remoteControl);
remoteControl.onButtonWasPressed(0);
remoteControl.offButtonWasPressed(0);
remoteControl.onButtonWasPressed(1);
remoteControl.undoButtonWasPushed();
remoteControl.onButtonWasPressed(2);
remoteControl.offButtonWasPressed(2);
remoteControl.undoButtonWasPushed();
}
}
原文地址:http://www.cnblogs.com/yanglf/p/3890841.html