未分类题

某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式的类图如图6-1所示。
中级软件设计师,历年真题,2014年下半年(下午)《软件设计师》真题
【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.light=light;}
public void execute(  ){(2);}
}
class LightOffCommand implements Command{//关灯命令
Light light;
public LightOffCommand(Light light){this.light=light;}
public void execute(  ){(3);}
}
class RemoteControl{//遥控器
Command[]onCommands=new Command[7];
Command[]offCommands=new Command[7];
public RemoteControl(  ){/*代码省略*/}
public void setCommand(int slot,Command onCommand,Command offCommand){
(4)=onCommand;
(5)=offCommand;
}
public void onButtonWasPushed(int slot){
(6);
}
public void offlButtonWasPushed(int slot){
(7);
}
}
class RemoteLoader{
public static void main(String[]args){
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);
}
}

【参考答案】

(1)interface Command
(2)light.on()
(3)light.off()......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

未分类题
某集团公司在全国不同城市拥有多个大型超市,为了有效管理各个超市的业务工作,需要构建一个超市信息管理系统。【需求分析结果】(1)超市信息包括:超市名称、地址、经理和电话,其中超市名称唯一确定超市关系的每一个元组。每个超市只有一名经理。(2)超市设有计划部、财务部、销售部等多个部门,每个部门只有一名部门经理,有多名员工,每个员工只属于一个部门。部门信息包括:超市名称、部门名称、部门经理和联系电话。超市名称、部门名称唯一确定部门关系的每一个元组。(3)员工信息包括:员工号、姓名、超市名称、部门名称、职位、联系方式和工资。其中,职位信息包括:经理、部门经理、业务员等。员工号唯一确定员工关系的每一个元组。(4)商品信息包括:商品号、商品名称、型号、单价和数量。商品号唯一确定商品关系的每一个元组。一名业务员可以负责超市内多种商品的配给,一种商品可以由多名业务员配给。【概念模型设计】根据需求分析阶段收集的信息,设计的实体联系图和关系模式(不完整)如下:图2-1实体联系图【关系模式设计】超市(超市名称,经理,地址,电话)部门((a),部门经理,联系电话)员工((b),姓名,联系方式,职位,工资)商品(商品号,商品名称,型号,单价,数量)配给((c),配给时间,配给数量,业务员)【问题1】(4分)根据问题描述,补充四个联系,完善图1-1的实体联系图。联系名可用联系1、联系2、联系3和联系4代替,联系的类型分为1:1、1:n和m:n(或1:1、1:*和*:*)。【问题2】(7分)(1)根据实体联系图,将关系模式中的空(a)~(c)补充完整;(2)给出部门和配给关系模式的主键和外键。【问题3】(4分)(1)超市关系的地址可以进一步分为邮编、省、市、街道,那么该属性是属于简单属性还是复合属性?请用100字以内文字说明。(2)假设超市需要增设一个经理的职位,那么超市与经理之间的联系类型应修改为(d),超市关系应修改为(e)。