命令模式代码及类图.doc_第1页
命令模式代码及类图.doc_第2页
命令模式代码及类图.doc_第3页
命令模式代码及类图.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

命令模式类图代码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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论