阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌灯具的开

admin2015-12-01  31

问题 阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
    【说明】
    某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式的类图如图5—1所示。

【java代码】
class Light{
public Light(){};
public Light(string name){/*代码省略*/)
public void on(){/*代码省略*/}//{开灯
public void off(){/*代码省略*/}  //关灯
}:
(1){
public void execute();
}
class LightOnCommand implements Command{//开灯命令
Light light;
public LightOnCommand(Light light){this.1ight=light;}
public Void execute(){(2);)
}
class LightoffCommandimplements Command{//关灯命令
  Light light;
  public LightOffCommand(Light light){this.1ight=light;)
  public Void execute(){(3);)
)
class RemoteControl{//遥控器
Command[]onCommands[7];
Command[]offCommands[7];
Public RemoteControl(){/*代码省略*/}
Publ ic void setCommand(int slot Command onCommand,CommandoffCommand){
(4)=onCommand:
(5)=offCommand:
}
Publ ic void onButtonWasPushed(int slot){(6);)
Public void offButtonWasPushed(int slot)((7);}
)
classremoteLoader{
publ ic static void main(string[]args){
RemoteControlremoteControl=new RemoteControl();
LightlivingRoomLight=new Light(“Living Room”);
Light kitchen Light=new Light(“kitchen”);
LightOnCommandlivingRoomLightOn=new LightOnCommand(livingRoomLight);
LightOffCommandlivingRoomLightOff=new LightOffCommand(livingRoomLight);
LightOnCommandkitchenLightOn=new LightOnCommand(kitchenLight);
LightOffCommandkitchenLightOff=new LightOffCommand(kitchenLight);
remoteControl.setCommand(0,livingRoomLightOn,livingRoomLightOff);
remoteControl.setCommand(1,kitchenLightOn,kitchenLightOff);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
}
}

选项

答案 (1)interface Command (2)light.on() (3)light.off() (4)onCommands[slot] (5)offCommands[slot] (6)onCommands[slot].execute() (7)offCommands[slot].execute()

解析 本题考查Command命令模式的概念及应用,并用Java语言实现。Command命令模式是一种对象行为模式。它主要解决的问题是,在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”的问题。将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
转载请注明原文地址:https://kaotiyun.com/show/tdDZ777K
0

相关试题推荐
最新回复(0)