首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读下列说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。 【说明】 某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌灯具的开关,现
阅读下列说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。 【说明】 某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌灯具的开关,现
admin
2016-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
软件设计师下午应用技术考试
软考中级
相关试题推荐
该网络采用R1~R7共7台路由器,采用动态路由协议OSPF。由图1-1可见,该网络共划分了3个OSPF区域,其主干区域为(1),主干区域中,(2)为区域边界路由器,(3)为区域内路由器。下表是该系统中路由器的IP地址分配表。请根据上
阅读以下说明,回答问题1至问题5,将解答填入对应的解答栏内。【说明】某公司内部服务器S1部署了重要的应用,该应用只允许特权终端PC1访问,如下图所示。为保证通信安全,需要在S1上配置相应的IPSec策略。综合考虑后,确定该IPSec策略如下。
为了使DNS_Server1能正确解析本地Web站点的域名,需对DNS_Server1中的DNS服务进行配置。在图1所示的对话框中,新建的区域名称是(1);在图2所示的对话框中,添加的新建主机名称为(2),IP地址栏应填入(3)。在客户端除了可以用p
为了使DNS_Server1能正确解析本地Web站点的域名,需对DNS_Server1中的DNS服务进行配置。在图1所示的对话框中,新建的区域名称是(1);在图2所示的对话框中,添加的新建主机名称为(2),IP地址栏应填入(3)。在网络A的PCI中执
请在(1)~(4)空白处填写恰当的内容。DHCP的工作过程是:1)IP租用请求。DHCP客户机启动后,发出一个DHCPDISCOVER消息,其封包的源地址为(1),目标地址为(2)。2)IP租用提供。当DHCP服务器收到DHCPDI
阅读以下说明,回答问题1至问题3,将解答填入对应的解答栏内。【说明】某校园网申请到了C类网络地址块202.115.0.0/24~202.115.3.0/24。根据网络规划需求,网络中心、图书馆、教学实验楼以及行政办公楼的各个部门需划分到不同网段。
根据你的网络工程经验,请用250字以内的文字简要描述该21层教学综合大楼网络层次结构设计的要点。(不要求画图)该21层教学综合大楼的部分网络拓扑结构如图1-22所示,其中L3_switch1、L3_switch2为该教学综合大楼的两台核心交换机;Swi
根据你的网络工程经验,请用250字以内的文字简要描述该21层教学综合大楼网络层次结构设计的要点。(不要求画图)请用300字以内的文字,以提纲形式描述该21层教学综合大楼综合布线设计的方案要点。
在RAS上存在着两个RJ45的端口,分别为“Console”与“AUX”,请问这两个端口的用途是什么?(控制在100个字以内)在调用超级终端程序进行设备连接时,应该对设备的连接参数进行正确设置,参数主要包括串口数据传输率、数据位数、停止位数以及是否有奇
请阅读以下说明和Socket程序,将应填入(n)处的字句写在对应栏内。【说明】网络应用的基本模型是客户机/服务器模型,这是一个不对称的编程模型,通信的双方扮演不同的角色:客户机和服务器。一般发起通信请求的应用程序称为客户软件,该应
随机试题
高钾血症比低钾血症更危险的原因是()
A.糖尿病合并肾小球硬化症B.肾性糖尿C.应激性糖尿D.1型糖尿病E.甲状腺功能亢进血糖升高,尿糖阳性,空腹血浆胰岛素水平明显降低
手术过程中较大动脉出血,应采用的止血方法是
工程咨询单位的要素包括()。
下列哪些货物属我国政府禁止进口的范围?()
一、注意事项1.申论考试,与传统作文考试不同,是对分析驾驭材料的能力与对表达能力并重的考试。2.作答参考时限:阅读资料40分钟,作答110分钟。3.仔细阅读给定的资料,按照后面提出的“申论要求”依次作答。二、给定资料1.2
互利共赢开放战略,主要是在更好地促进中国自身发展的同时,兼顾各方特别是发展中国家的正当利益,切实承担起相应的国际责任。实施互利共赢的开放战略符合世界各国同舟共济的客观要求,确立互利共赢的思想基础是
MadeinChinavs.CreatedinChina中国制造vs.中国创造Writeanessayof160-200wordsbasedonthedrawing.Inyouressay,youshould1
在一个VisualFoxPro数据表文件中有2个通用字段和3个备注字段,该数据表的备注文件数目是
"Salty"RicePlantBoostsHarvestsBritishscientistsarebreedinganewgenerationofriceplantsthatwillbeabletogrow
最新回复
(
0
)