命令模式代码及类图

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

命令模式类图

代码

public interface Command {

public void execute();

}

public class NoCommand implements Command{

public void execute(){} }

public class RemoteControl { Command[] onCommands;

Command[] offCommands;

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;

}

}

public void setCommand(int

slot,Command onCommand,Command offCommand){

onCommands[slot]=onCommand;

offCommands[slot]=offCommand;

}

public void

onButtonWasPushed(int slot){

onCommands[slot].execute();

}

public void OffBottonPushed(int slot){

offCommands[slot].execute();

}

public String toString(){

StringBuffer stringBuff=new StringBuffer();

stringBuff.append("\n------ Remote Control ------\n");

for(int

i=0;i

stringBuff.append("[slot "+ i+ "] "+

onCommands[i].getClass().getName()

+"

"+offCommands[i].getClass().getNam

e()+"\n");

}

return

stringBuff.toString();

}

}

public class Light {

String name;

public Light(){}

public Light(String name){

=name;

}

public void on(){

System.out.println(name+" light is on");

}

public void off(){

System.out.println(name+" light is off");

}

}

public class LightOnCommand implements Command{

Light light;

public LightOnCommand(Light light){

this.light=light;

}

public void execute(){

light.on();

}

}

public class LightOffCommand implements Command{

Light light;

public LightOffCommand(Light light){

this.light=light;

}

public void execute(){

light.off();

}

}

public class Stereo {

String name;

public Stereo(String name){ =name;

}

public void on(){

System.out.println(name+" Stereo On");

}

public void off(){

System.out.println(name+" Stereo Off");

}

public void setCD(){

System.out.println(name+" Stereo set CD");

}

public void setDvd(){

System.out.println(name+" Stereo set Dvd");

}

public void setRadio(){

System.out.println(name+" Stereo set radio");

}

public void setVolume(){

System.out.println(name+" Stereo set volume");

}

}

public class StereoOffCommand implements Command{

Stereo stereo;

public StereoOffCommand(Stereo stereo){

this.stereo=stereo;

}

public void execute() {

stereo.off();

}

}

public class StereoOnWithCDCommand implements Command{

Stereo stereo;

public

StereoOnWithCDCommand(Stereo stereo){

this.stereo=stereo;

}

public void execute() {

stereo.on();

stereo.setCD();

stereo.setVolume();

}

}

public class RemoteLoader {

public static void

main(String[]args){

RemoteControl

remoteControl=new RemoteControl();

Light livingRoomLight =new Light("Living Room");

Light kitchenLight=new

Light("Kitchen");

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);

StereoOnWithCDCommand stereoOnWithCD=new StereoOnWithCDCommand(stereo);

StereoOffCommand

stereoOff=new

StereoOffCommand(stereo);

remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);

remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);

remoteControl.setCommand(2, stereoOnWithCD, stereoOff);

System.out.println(remoteContro l);

remoteControl.onButtonWasPushed (0);

remoteControl.OffBottonPushed(0 );

相关文档
最新文档