public interface Command {
public void execute();
public void undo();
}
public class LightOnCommand implements Command{
private Light light;
public LightOnCommand(Light light){
this.light=light;
}
@Override
public void execute() {
light.On();
}
@Override
public void undo() {
}
}
public class LightOffCommand implements Command{
private Light light;
public LightOffCommand(Light light){
this.light=light;
}
@Override
public void execute() {
light.Off();
}
@Override
public void undo() {
light.On();
}
}
public class Stereo {
static int volume=0;
public void On(){
System.out.println("Stereo On");
}
public void Off(){
System.out.println("Stereo Off");
}
public void SetCd(){
System.out.println("Stereo SetCd");
}
public void SetVol(int vol){
volume=vol;
System.out.println("Stereo volume="+volume);
}
public int GetVol(){
return volume;
}
public void Start(){
System.out.println("Stereo Start");
}
}
public class StereoAddVolCommand implements Command{
private Stereo stereo;
public StereoAddVolCommand(Stereo stereo){
this.stereo=stereo;
}
@Override
public void execute() {
int vol=stereo.GetVol();
if(vol<11){
stereo.SetVol(++vol);
}
}
@Override
public void undo() {
// TODO Auto-generated method stub
}
}
public class StereoOnCommand implements Command{
private Stereo stereo;
public StereoOnCommand(Stereo stereo){
this.stereo=stereo;
}
@Override
public void execute() {
stereo.On();
stereo.SetCd();
}
@Override
public void undo() {
// TODO Auto-generated method stub
}
}
public class NoCommand implements Command{
@Override
public void execute() {
// TODO Auto-generated method stub
}
@Override
public void undo() {
// TODO Auto-generated method stub
}
}
public interface Control {
public void onButton(int slot);
public void offButton(int slot);
public void undoButton();
}
public class CommandModeControl implements Control{
private Command[] onCommands;
private Command[] offCommands;
public CommandModeControl(){
onCommands=new Command[5];
offCommands=new Command[5];
Command noCommand=new NoCommand();
for(int i=0,len=onCommands.length;i<len;i++){
onCommands[i]=noCommand;//初始化
offCommands[i]=noCommand;
}
}
public void setCommand(int slot,Command onCommand,Command offCommand){
onCommands[slot]=onCommand;
offCommands[slot]=offCommand;
}
public void onButton(int slot){
onCommands[slot].execute();
}
public void offButton(int slot){
offCommands[slot].execute();
}
}
public class ControlTest {
public static void main(String[] args) {
CommandModeControl control=new CommandModeControl();
Light bedroomlight=new Light("BedRoom");
Light kitchlight=new Light("Kitch");
Stereo stereo=new Stereo();
LightOnCommand bedroomlighton=new LightOnCommand(bedroomlight);
LightOffCommand bedroomlightoff=new LightOffCommand(bedroomlight);
LightOnCommand kitchlighton=new LightOnCommand(kitchlight);
LightOffCommand kitchlightoff=new LightOffCommand(kitchlight);
StereoOnCommand stereoOn=new StereoOnCommand(stereo);
//StereoOffCommand stereoOff=new StereoOffCommand(stereo);
StereoAddVolCommand stereoaddvol=new StereoAddVolCommand(stereo);
control.setCommand(0, bedroomlighton,bedroomlightoff);
control.setCommand(1, kitchlighton, kitchlightoff);
control.setCommand(2, stereoOn, null);
control.setCommand(3, stereoaddvol, null);
control.onButton(0);
control.offButton(0);
control.onButton(1);
control.offButton(1);
control.onButton(2);
control.onButton(3);
control.offButton(3);
control.offButton(2);
}
}