深入浅出设计模式之命令模式.ppt_第1页
深入浅出设计模式之命令模式.ppt_第2页
深入浅出设计模式之命令模式.ppt_第3页
深入浅出设计模式之命令模式.ppt_第4页
深入浅出设计模式之命令模式.ppt_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

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

文档简介

命令模式 封装调用 一个例子 publicvoidactionPerformed ActionEvente Objectobj e getSource if obj mnuOpen fileOpen openfileif obj mnuExit exitClicked exitfromprogramif obj btnRed redClicked turnred privatevoidexitClicked System exit 0 privatevoidfileOpen FileDialogfDlg newFileDialog this Openafile FileDialog LOAD fDlg show privatevoidredClicked p setBackground Color red 上述程序设计 当按钮和菜单项不多的时候工作良好 但按钮和菜单项多时 就不好办了 publicinterfaceCommand publicvoidExecute publicvoidactionPerformed ActionEvente Commandcmd Command e getSource cmd Execute 这样 我们需要给每一个对象提供一个执行的方法 命令模式 theseprogramobjectsshouldbecompletelyseparatefromeachotherandshouldnothavetoknowhowotherobjectswork TheuserinterfacereceivesacommandandtellsaCommandobjecttocarryoutwhateverdutiesithasbeeninstructedtodo TheUIdoesnotandshouldnotneedtoknowwhattaskswillbeexecuted 命令模式 程序对象应该彻底的彼此解耦 不需要知道其他的对象是如何工作的 用户界面接收到命令然后告诉命令对象执行设定的工作 用户界面不知道也不应该知道命令是如何执行的 命令的发送者 命令的接受者 命令对象 解耦了命令的发送者和命令的接受者 饭店用餐 顾客点了饭菜 形成一个订单 侍者将订单交给厨房 厨房根据订单配菜 侍者无需知道订单的内容 饭菜的制作者和饭菜的消费者完全分开 彼此无需直接联系 遥控器的例子 遥控器需要控制每一个电器的动作 如电灯 电扇 电视机 同是开 动作不一样 我们设置命令接口publicinterfaceCommand publicvoidexecute publicclassLightOnCommandimplementsCommand Lightlight publicLightOnCommand Lightlight this light light publicvoidexecute light on 命令的接收对象 命令接收对象自己执行 命令对象 接收对象实现了命令接口 Command publicclassSimpleRemoteControl Commandslot publicSimpleRemoteControl publicvoidsetCommand Commandcommand slot command publicvoidbuttonWasPressed slot execute 遥控器测试 publicclassRemoteControlTest publicstaticvoidmain String args SimpleRemoteControlremote newSimpleRemoteControl Lightlight newLight LightOnCommandlightOn newLightOnCommand light remote setCommand lightOn remote buttonWasPressed 命令模式 将请求封装成对象 使用不同的请求 队列或日志来参数化其他对象 命令模式支持可撤销操作 将请求封装成对象 什么对象 对象将接受者和动作包在内部 只有一个execute接口 外部调用这个接口 不知会进行什么样的操作 多功能遥控器 需要控制多个设备 每一个设备都有开关按钮需要一个命令组 来控制设备组 起居室灯 厨房灯 吊扇 车库门 音响 等 publicclassRemoteControl Command onCommands Command offCommands publicRemoteControl onCommands newCommand 7 offCommands newCommand 7 CommandnoCommand newNoCommand for inti 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 publicvoidoffButtonWasPushed intslot offCommands slot execute publicclassLightOffCommandimplementsCommand NoCommand 它是一个空对象 称监视对象 避免了判断if onCommand slot null onCommand slot execute 撤销命令 有时应该允许后悔 允许命令撤销 publicinterfaceCommand publicvoidexecute publicvoidundo publicclassLightOffCommandimplementsCommand Lightlight publicLightOffCommand Lightlight this light light publicvoidexecute light off publicvoidundo light on 使用撤销命令的遥控器 代码 测试 代码 使用状态实现撤销 吊扇代码 加入撤销到吊扇的命令类 代码其它几个low medium off 如何实现 测试吊扇类 代码 遥控器的party模式 产生一个新的命令 可以让所有设备打开 publicclassMacroCommandimplementsCommand Command commands publicMacroCommand Command commands mands commands publicvoidexecute for inti 0 i commands length i commands i execute publicvoidundo for inti 0 i commands length i commands i undo 测试宏命令

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论