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

admin2015-12-01  22

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

    【C++代码】
    class Light{
    public:
    Light(string name){/*代码省略*/}
    void on(){/*代码省略*/}  //开灯
    void off(){/*代码省略*/}  //关灯
}
class Command{
public:
(1);
);
class LightOnCommand:public Command{//开灯命令
private:
  Light*light;
public:
  LightOnCommand(Light*light){this一>iight=light;)
  Void execute(){(2);)
};
class LightOffCommand:public Command{//关灯命令
private:
  Light*iight;
public:
  LightOffCommand(Light*light){this一>i ight=light;)
  Void execute(){(3);}
};
class RemoteControl(//遥控器
private:
Command*onCommands[7];
Command*offCommands[7];
public:
RemoteControl(){/*代码省略*/)
void setCommand(int slot Command*onCommand,Command*offCommand){
(4)=onCommand;
(5)=offCommand;
}
void onButtonWasPushed(int slot){(6):)
void offButtonwasPushed(int slot){(7):)
);
int main(){
RemoteControl*remoteControl=new RemoteControl();
    Light*livingRoomLight=new Light(“Living Room”);
    Light*kitchen Light=new Light(“kitchen");
    LightOnCommand*IivingRoomLightOn=new LightOnCommand(livingRoomLight):
    LightOffCommand*iivingRoomLightOff=new LightOffCommand(livingRoomLight):
    LightOnCommand*kitchenLightOn=new LightOnCommand(kitchenLight):
    LightOffCommand*kitchenLightOff=new LightOffCommand(kitchenLight):
    remoteControl一>setCommand(0,livingRoomLightOn,livingRoomLightOff);
    remoteControl一>setCommand(1,kitchenLightOn,kitchenLightOff);
    remoteControl一>onButtonWasPushed(0):
    remoteControl一>off ButtonWasPushed(0);
    remoteControl一>onButtonWasPushed(1);
    remoteControl一>offButtonWasPushed(1);
    /*其余代码省略*/
    return0;
    }

选项

答案 (1)void execute(){} (2)light一>on() (3)light一>off() (4)onCommands[slot] (5)offCommands[slot] (6)onCommands[slot]一>execute() (7)offCommands[slot]一>execute()

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

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