命令模式代码及类图

(4页)

'命令模式代码及类图'
命令模式类图代码4public interface Command { public void execute();}public class NoCommand implements Command{ public void execute(){}}public class RemoteControl { Command[] onCommands; Command[] offCommands; public RemoteControl(){ onCommands=new Command[7]; offCommands=new Command[7]; Command noCommand=new NoCommand(); for(int i=0;i<7;i++){ onCommands[i]=noCommand; offCommands[i]=noCommand; } } public void setCommand(int slot,Command onCommand,Command offCommand){ onCommands[slot]=onCommand; offCommands[slot]=offCommand; } public void onButtonWasPushed(int slot){ onCommands[slot].execute(); } public void OffBottonPushed(int slot){ offCommands[slot].execute(); } public String toString(){ StringBuffer stringBuff=new StringBuffer(); stringBuff.append("\n------ Remote Control ------\n"); for(int i=0;i<onCommands.length;i++){ stringBuff.append("[slot "+ i+ "] "+ onCommands[i].getClass().getName() +" "+offCommands[i].getClass().getName()+"\n"); } return stringBuff.toString(); }}public class Light { String name; public Light(){} public Light(String name){ this.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 Command{ Light light; public LightOnCommand(Light light){ this.light=light; } public void execute(){ light.on(); }}public class LightOffCommand implements Command{ Light light; public LightOffCommand(Light light){ this.light=light; } public void execute(){ light.off(); }}public class Stereo { String name; public Stereo(String name){ this.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 Command{ Stereo stereo; public StereoOffCommand(Stereo stereo){ this.stereo=stereo; } public void execute() { stereo.off(); }}public class StereoOnWithCDCommand implements Command{ Stereo stereo; public StereoOnWithCDCommand(Stereo stereo){ this.stereo=stereo; } public void execute() { stereo.on(); stereo.setCD(); stereo.setVolume(); }}public class RemoteLoader { public static void main(String[]args){ RemoteControl remoteControl=new RemoteControl(); Light livingRoomLight =new Light("Living Room"); Light kitchenLight=new Light("Kitchen"); Stereo stereo=new Stereo("Living Room"); LightOnCommand livingRoomLightOn=new LightOnComm
关 键 词:
命令 模式 代码
 天天文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:命令模式代码及类图
链接地址: https://www.wenku365.com/p-44814938.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服点击这里,给天天文库发消息,QQ:1290478887 - 联系我们

本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有【成交的100%(原创)】。本站是网络服务平台方,若您的权利被侵害,侵权客服QQ:1290478887 欢迎举报。

1290478887@qq.com 2017-2027 https://www.wenku365.com 网站版权所有

粤ICP备19057495号 

收起
展开