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

admin2016-05-10  28

问题 阅读下列说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
    某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式的类图如图5—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一>light=light;  }
    void execute(){    (2)    ;  )
    };
    class LightOffCommand:public Command{  //关灯命令
    private:
    Light*light;
    public:
    LightOffCommand(Light*light){this->light=light;  )
    void execute(){    (3)    ;  }
};
    class RemoteControl{  //遥控器
    private:
    Command*onCommands[7];
    Command*OffCommands[7];
    public:
    RemoteControl()  {  /*代码省略*/  }
    void setCommand(int S lot,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*  kitchenLight=new Light(”kitchen”);
    LightOnCommand*livingRoomLightOn=new LightOnCommand  (liVingRoomLight);
    LightOffCommand*livingRoomLightOff=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一>offButtonWasPushed(0);
    remoteControl一>onButtonWasPushed(1);
    remoteControl一>offButtonWaSPushed(1);
    /*其余代码省略*/
    return 0;
    }

选项

答案(1)virtual void execute()=0 (2)light一>on() (3)light一>oif() (4)onCommands[slot] (5)offCommands[slot] (6)onCommands[slot]->execute() (7)offCommands[slot]一>execute()

解析 本题考查命令(Command)模式的基本概念和应用。
    命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。
在软件系统中,行为请求者与行为实现者之间通常呈现一种紧耦合的关系。但在某些场合,比如要对行为进行记录撤销重做事务等处理,这种无法抵御变化的紧耦合是不合适的。这种情况下,使用Command模式将行为请求者与行为实现者进行解耦。
    题目中给出了Command模式的类图,其中:
    Command类为所有命令声明了一个接口。调用命令对象的execute()方法,就可以让接收者进行相关的动作。
    ConcreteCommand类定义了动作和接收者之间的绑定关系。调用者只要调用execute()就可以发出请求,然后由ConcreteCommand调用接收者的一个或多个动作。
    Invoker持有一个命令对象,并在某个时间点调用命令对象的execute()方法,将请求付诸实行。
    Receiver知道如何进行必要的工作,实现这个请求。任何类都可以当接收者。
    了解了Command模式的内涵之后,下面来看程序。
    由于Command类的主要作用是为所有的ConcreteCommand定义统一的接口,在C++中通常采用抽象类来实现。C++的抽象类是至少具有一个纯虚拟函数的类。本题中的接口就是execute()方法,所以(1)处要求填写的是纯虚拟函数execute的定义方式,即virtualvoid execute()=0。
    类LightOnCommand、LightOffCommand对应的就是模式中的ConcreteCommand。ConcreteCommand中execute()方法的代码在类图中已经给出,现在需要确定receiver是谁。类Light充当的是Receiver,其中定义了两种action:on和off。所以(2)、(3)对应代码分别为light一>on()、light->off()。
    类RemoteControl对应的是模式中的Invoker,在该类中设置需要控制的命令对象。
(4)处对应的代码为onCommands[slot],设置“开灯”命令对象;(5)处对应的代码为offCommands[slot],设置“关灯”,命令对象。类RemoteControl中的方法onButtonWasPushed和offButtonWasPushed,分别完成对开灯、关灯命令对象的execute方法的调用,所以(6)、(7)处分别对应代码onCommands[slot]->execute()、offCommands[slot]一>execute()。
转载请注明原文地址:https://jikaoti.com/ti/lsi7FFFM
0

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