欢迎来到天天文库
浏览记录
ID:44814938
大小:50.00 KB
页数:4页
时间:2019-10-29
《命令模式代码及类图》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、命令模式类图代码4publicinterfaceCommand{publicvoidexecute();}publicclassNoCommandimplementsCommand{publicvoidexecute(){}}publicclassRemoteControl{Command[]onCommands;Command[]offCommands;publicRemoteControl(){4onCommands=newCommand[7];offCommands=newCommand[7];CommandnoCommand=newNoCommand();for(int
2、i=0;i<7;i++){onCommands[i]=noCommand;offCommands[i]=noCommand;}}publicvoidsetCommand(intslot,CommandonCommand,CommandoffCommand){onCommands[slot]=onCommand;offCommands[slot]=offCommand;}publicvoidonButtonWasPushed(intslot){onCommands[slot].execute();}publicvoidOffBottonPushed(intslot){offCom
3、mands[slot].execute();}publicStringtoString(){StringBufferstringBuff=newStringBuffer();stringBuff.append("------RemoteControl------");for(inti=0;i4、nstringBuff.toString();}}44publicclassLight{Stringname;publicLight(){}publicLight(Stringname){this.name=name;}publicvoidon(){System.out.println(name+"lightison");}publicvoidoff(){System.out.println(name+"lightisoff");}}publicclassLightOnCommandimplementsCommand{Lightlight;publicLightOnComman5、d(Lightlight){this.light=light;}publicvoidexecute(){light.on();}}publicclassLightOffCommandimplementsCommand{Lightlight;publicLightOffCommand(Lightlight){this.light=light;}publicvoidexecute(){light.off();}}44publicclassStereo{Stringname;publicStereo(Stringname){this.name=name;}publicvoidon()6、{System.out.println(name+"StereoOn");}publicvoidoff(){System.out.println(name+"StereoOff");4}publicvoidsetCD(){System.out.println(name+"StereosetCD");}publicvoidsetDvd(){System.out.println(name+"StereosetDvd");}publicvoidsetRadio(){System.out.println(name+"Stereosetradio");}publicvoidsetVolu7、me(){System.out.println(name+"Stereosetvolume");}}publicclassStereoOffCommandimplementsCommand{Stereostereo;publicStereoOffCommand(Stereostereo){this.stereo=stereo;}publicvoidexecute(){stereo.off();}}publicclassStereoOnWithCDCommandimplementsComman
4、nstringBuff.toString();}}44publicclassLight{Stringname;publicLight(){}publicLight(Stringname){this.name=name;}publicvoidon(){System.out.println(name+"lightison");}publicvoidoff(){System.out.println(name+"lightisoff");}}publicclassLightOnCommandimplementsCommand{Lightlight;publicLightOnComman
5、d(Lightlight){this.light=light;}publicvoidexecute(){light.on();}}publicclassLightOffCommandimplementsCommand{Lightlight;publicLightOffCommand(Lightlight){this.light=light;}publicvoidexecute(){light.off();}}44publicclassStereo{Stringname;publicStereo(Stringname){this.name=name;}publicvoidon()
6、{System.out.println(name+"StereoOn");}publicvoidoff(){System.out.println(name+"StereoOff");4}publicvoidsetCD(){System.out.println(name+"StereosetCD");}publicvoidsetDvd(){System.out.println(name+"StereosetDvd");}publicvoidsetRadio(){System.out.println(name+"Stereosetradio");}publicvoidsetVolu
7、me(){System.out.println(name+"Stereosetvolume");}}publicclassStereoOffCommandimplementsCommand{Stereostereo;publicStereoOffCommand(Stereostereo){this.stereo=stereo;}publicvoidexecute(){stereo.off();}}publicclassStereoOnWithCDCommandimplementsComman
此文档下载收益归作者所有