深入浅出设计模式之命令模式

上传人:野鹰 文档编号:26879360 上传时间:2018-01-03 格式:PPT 页数:31 大小:172.50KB
返回 下载 相关 举报
深入浅出设计模式之命令模式_第1页
第1页 / 共31页
深入浅出设计模式之命令模式_第2页
第2页 / 共31页
深入浅出设计模式之命令模式_第3页
第3页 / 共31页
深入浅出设计模式之命令模式_第4页
第4页 / 共31页
深入浅出设计模式之命令模式_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《深入浅出设计模式之命令模式》由会员分享,可在线阅读,更多相关《深入浅出设计模式之命令模式(31页珍藏版)》请在金锄头文库上搜索。

1、命令模式,封装调用,一个例子,public void actionPerformed(ActionEvent e) Object obj = e.getSource();if(obj = mnuOpen) fileOpen(); /open fileif (obj = mnuExit) exitClicked(); /exit from programif (obj = btnRed) redClicked(); /turn redprivate void exitClicked() System.exit(0);private void fileOpen() FileDialog fDlg

2、= new FileDialog(this, Open a file,FileDialog.LOAD);fDlg.show();private void redClicked() p.setBackground(Color.red);,上述程序设计,当按钮和菜单项不多的时候工作良好,但按钮和菜单项多时,就不好办了。,public interface Command public void Execute();public void actionPerformed(ActionEvent e) Command cmd = (Command)e.getSource();cmd.Execute();

3、这样,我们需要给每一个对象提供一个执行的方法。,命令模式,these program objects should be completely separate from each other and should not have to know how other objects work. The user interface receives a command and tells a Command object to carry out whatever duties it has been instructed to do. The UI does not and should

4、not need to know what tasks will be executed.,命令模式,程序对象应该彻底的彼此解耦,不需要知道其他的对象是如何工作的。用户界面接收到命令然后告诉命令对象执行设定的工作,用户界面不知道也不应该知道命令是如何执行的。,命令的发送者,命令的接受者,命令对象,解耦了命令的发送者和命令的接受者,饭店用餐,顾客点了饭菜,形成一个订单,侍者将订单交给厨房,厨房根据订单配菜。侍者无需知道订单的内容。饭菜的制作者和饭菜的消费者完全分开,彼此无需直接联系。,遥控器的例子,遥控器需要控制每一个电器的动作,如电灯,电扇,电视机,同是开,动作不一样。我们设置命令接口 pub

5、lic interface Command public void execute(); ,public class LightOnCommand implements Command Light light;public LightOnCommand(Light light) this.light = light;public void execute() light.on();,命令的接收对象,命令接收对象自己执行,命令对象,接收对象实现了命令接口,Command,public class SimpleRemoteControl Command slot; public SimpleRem

6、oteControl() public void setCommand(Command command) slot = command; public void buttonWasPressed() slot.execute();,遥控器测试,public class RemoteControlTest public static void main(String args) SimpleRemoteControl remote = new SimpleRemoteControl();Light light = new Light();LightOnCommand lightOn = new

7、LightOnCommand(light);remote.setCommand(lightOn);remote.buttonWasPressed(); ,命令模式,将请求封装成对象,使用不同的请求、队列或日志来参数化其他对象。命令模式支持可撤销操作。将请求封装成对象,什么对象?对象将接受者和动作包在内部,只有一个execute接口,外部调用这个接口,不知会进行什么样的操作。,多功能遥控器,需要控制多个设备,每一个设备都有开关按钮需要一个命令组,来控制设备组:起居室灯,厨房灯,吊扇,车库门,音响,等,public class RemoteControl Command onCommands;Co

8、mmand offCommands; public RemoteControl() onCommands = new Command7;offCommands = new Command7; Command noCommand = new NoCommand();for (int i = 0; i 7; i+) onCommandsi = noCommand;offCommandsi = noCommand;,public void setCommand(int slot, Command onCommand, Command offCommand) onCommandsslot = onCo

9、mmand;offCommandsslot = offCommand;public void onButtonWasPushed(int slot) onCommandsslot.execute(); public void offButtonWasPushed(int slot) offCommandsslot.execute();,public class LightOffCommand implements Command ?,NoCommand,它是一个空对象,称监视对象,避免了判断 if(onCommandslot != null) onCommandslot.execute();,

10、撤销命令,有时应该允许后悔,允许命令撤销。public interface Command public void execute();public void undo();,public class LightOffCommand implements Command Light light; public LightOffCommand(Light light) this.light = light; public void execute() light.off(); public void undo() light.on();,使用撤销命令的遥控器,代码,测试,代码,使用状态实现撤销,

11、吊扇代码,加入撤销到吊扇的命令类,代码其它几个low,medium,off.如何实现。,测试吊扇类,代码,遥控器的party模式,产生一个新的命令,可以让所有设备打开。public class MacroCommand implements Command Command commands; public MacroCommand(Command commands) mands = commands; public void execute() for (int i = 0; i commands.length; i+) commandsi.execute(); public void undo() for (int i = 0; i commands.length; i+) commandsi.undo();,测试宏命令,代码,命令模式的优点和缺点:,优点:降低Client和命令接受者的耦合,是命令请求和命令执行的对象分割便于修改和扩张便于聚合多个命令缺点:造成出现过多的具体命令类,太多文件。,命令模式,将命令发送者和命令解释者解耦两者的联系通过命令对象来中介的命令对象包含接受者和execute方法命令可以被撤销宏命令是一种简单的命令,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号