全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
命令模式类图代码4public interface Command public void execute();public class NoCommand implements Commandpublic void execute()public class RemoteControl Command onCommands;Command offCommands;public RemoteControl()onCommands=new Command7;offCommands=new Command7;Command noCommand=new NoCommand();for(int i=0;i7;i+)onCommandsi=noCommand;offCommandsi=noCommand;public void setCommand(int slot,Command onCommand,Command offCommand)onCommandsslot=onCommand;offCommandsslot=offCommand;public void onButtonWasPushed(int slot)onCommandsslot.execute();public void OffBottonPushed(int slot)offCommandsslot.execute();public String toString()StringBuffer stringBuff=new StringBuffer();stringBuff.append(n- Remote Control -n);for(int i=0;ionCommands.length;i+)stringBuff.append(slot + i+ + onCommandsi.getClass().getName()+ +offCommandsi.getClass().getName()+n);return stringBuff.toString();public class Light String name;public Light()public Light(String name)=name;public void on()System.out.println(name+ light is on);public void off()System.out.println(name+ light is off);public class LightOnCommand implements CommandLight light;public LightOnCommand(Light light)this.light=light;public void execute()light.on();public class LightOffCommand implements CommandLight light;public LightOffCommand(Light light)this.light=light;public void execute()light.off();public class Stereo String name;public Stereo(String name)=name;public void on()System.out.println(name+ Stereo On);public void off()System.out.println(name+ Stereo Off);public void setCD()System.out.println(name+ Stereo set CD);public void setDvd()System.out.println(name+ Stereo set Dvd);public void setRadio()System.out.println(name+ Stereo set radio);public void setVolume()System.out.println(name+ Stereo set volume);public class StereoOffCommand implements CommandStereo stereo;public StereoOffCommand(Stereo stereo)this.stereo=stereo;public void execute() stereo.off();public class StereoOnWithCDCommand implements CommandStereo stereo;public StereoOnWithCDCommand(Stereo stereo)this.stereo=stereo;public void execute() stereo.on();stereo.setCD();stereo.setVolume();public class RemoteLoader public static void main(Stringargs)RemoteControl remoteControl=new RemoteControl();Light livingRoomLight =new Light(Living Room);Light kitchenLight=new Light(Kitchen);Stereo stereo=new Stereo(Living Room);LightOnCommand livingRoomLightOn=new LightOnCommand(livingRoomLight);LightOffCommand livingRoomLightOff=new LightOffCommand(livingRoomLight);LightOnCommand kitchenLightOn=new LightOnCommand(kitchenLight);LightOffCommand kitchenLightOff=new LightOffCommand(kitchenLight);StereoOnWithCDCommand stereoOnWithCD=new StereoOnWithCDCommand(stereo);StereoOffCommand stereoOff=new StereoOffCommand(stereo);remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);remoteControl.setCommand(2, stereoOnWithCD, stereoOff);System.out.println(remoteControl);remoteControl.onButtonWasPushed(0);remoteControl.OffBottonPushed(0);remoteControl.onButtonWasPushed(1);remoteControl.OffBottonPushed(1);remoteControl.onButtonWasPushed(2);remoteControl.OffBottonPushed(2);测试结果- Remote Control -slot 0 LightOnCommand LightOffCommandslot 1 LightOnCommand LightOffCommandslot 2 StereoOnWithCDCommand StereoOffCommandslot 3 NoCommand NoCommandslot 4 NoCommand NoCommandslot 5 NoCommand NoCommandslot 6 NoCommand NoCommandLiv
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 货物延时卸货协议书
- 货物短期运输协议书
- 购房协议没网签合同
- 调取数据授权协议书
- 钢材供应合同协议书
- 购房签了定金协议书
- 供销社三农协议书
- 购买路灯材料协议书
- 2025年杭州市拱墅区拱宸桥街道招聘编外工作人员1人备考题库及答案详解(基础+提升)
- 钢厂限产协议书范本
- 兴国县2025年招聘城市社区专职网格员【23人】考试模拟卷附答案解析
- 2025年小学生日常行为规范、中小学生守则知识竞赛题(附答案)
- 重庆机场集团招聘笔试题型及答案
- 槟榔的危害课件
- 雪茄基础知识培训课件
- 投资研发产品协议书
- 糖尿病酮症酸中毒(DKA)合并高渗状态补液与血糖控制方案
- 2025年中国沥青行业发展研究报告
- 河南2025年河南省农村信用社(农商银行)员工招聘考试参考题库及答案
- 高校课程资源库建设方案与管理办法
- 2025年海运代理招聘面试题库及参考答案
评论
0/150
提交评论