已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Command命令模式介绍:Command命令模式是一种对象行为型模式,它主要解决的问题是:在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”的问题。如下图:有时我们必须向某对象提交请求,但并不知道关于被请求的操作或请求的接受者的任何信息,此时无法抵御变化的紧耦合是不合适的。如:需要对行为进行“记录、撤销/重做、事务”等处理。我们所要做的是将依赖关系转化,将紧耦合变为松耦合。则上图的形式转化为如下形式: Command模式通过将请求本身变成一个对象来使行为请求者可向未指定的应用对象提出请求。 GoF设计模式中说道:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。Command命令模式结构: 定义场景: 现在来看一个场景:对于notepad大家都很熟悉,在我们使用notepad打开一个文档之后,往往做一些操作,如;输入字符(Write)、删除前一个字符(Delete)、撤销刚才的操作(UnDo)。现在我们就用Console程序模拟这个过程。代码实现与结构分析: 在实现代码前先说明实现Command模式需要烤炉的一些问题:1、 一个命令对象应达到何种智能程度:命令对象的能力可大可小。这样就出现了两个极端。一是:它仅确定一个接收者和执行该请求的动作;一是:它自己实现所有功能,根本不需要额外的接收者对象。我给他们起了便于我方便记忆的名字,第一种叫OperationCommand,第二种叫ObjectCommand。当然只是为了便于记忆和理解,如有不理解,在代码实现与结构分析最后我会再谈谈我的想法,如有不妥,还请多提意见,一会在下面的代码中会分别对这两种情况进行示范。2、 支持取消和重做:为了达到这个目的ConcreteCommand类中要存储额外的状态信息。也就是上图中ConcreteCommand的state属性。3、 避免取消操作中过程中的错误积累:由于命令重复的执行、取消执行和重执行的过程可能会积累错误,以致一个应用的状态最终偏离初始值。这就有必要在Command中存入更多的信息以保证这些对象可被精确的复原。下面来看看代码上的实现:首先,我先作一个OperationCommand的例子,先做一个请求的接收者Document(也就是结构图中的Receiver)class Document public string strContent; public Document() strContent = ; 在这个程序中我们还要定义一个抽象类Command,对于OperationCommand类型来说,它仅确定一个接收者和执行该请求的动作。所以,在抽象类Command中,只声明一个Excute的方法。这个方法在其子类中进行实现。(当然这个Command还可以定义成接口)abstract class Command public Command() public abstract void Excute();接下来,就要实现各种操作(结构图中的ConcreteCommand),代码如下/写操作class WriteCommand : Command Document doc; ArrayList ObjectState; public WriteCommand(Document doc, ArrayList state) this.doc = doc; ObjectState = state; public override void Excute() doc.strContent += Console.ReadLine(); ObjectState.Add(doc.strContent); /删除操作 class DeleteCommand : Command Document doc; ArrayList ObjectState; public DeleteCommand(Document doc, ArrayList state) this.doc = doc; ObjectState = state; public override void Excute() doc.strContent = doc.strContent.Substring(0, doc.strContent.Length - 1); ObjectState.Add(doc.strContent); /撤销操作 class UnDoCommand : Command Document doc; ArrayList ObjectState; public UnDoCommand(Document doc, ArrayList state) this.doc = doc; ObjectState = state; public override void Excute() doc.strContent = (string)ObjectStateObjectState.Count - 2; ObjectState.Add(doc.strContent); 实现了各种操作后,编写一个客户代码进行测试class Program static void Main(string args) Document doc = new Document(); Console.WriteLine(Please Input next operation:); string strOperation = Console.ReadLine(); Command com = null; ArrayList ObjectState = new ArrayList();/Record state while (strOperation != Exit) switch (strOperation.ToLower() case write: com = new WriteCommand(doc, ObjectState); com.Excute(); Console.WriteLine(Write Operation: + doc.strContent); break; case del: com = new DeleteCommand(doc, ObjectState); com.Excute(); Console.WriteLine(Delete Operation: + doc.strContent); break; case undo: com = new UnDoCommand(doc, ObjectState); com.Excute(); Console.WriteLine(UnDo Operation: + doc.strContent); break; default: Console.WriteLine(Wrong Operation:); break; Console.WriteLine(Please Input next operation:); strOperation = Console.ReadLine(); 测试结果:Please Input next operation:writekWrite Operation:kPlease Input next operation:writeiWrite Operation:kiPlease Input next operation:writedWrite Operation:kidPlease Input next operation:writedWrite Operation:kiddPlease Input next operation:delDelete Operation:kidPlease Input next operation:undoUnDo Operation:kiddPlease Input next operation:下面再来实现以下ObjectCommand的例子,首先还是编写一个已存在的请求接收者Document(结构图中的Receiver)class Document public string strContent; public Document() strContent = ; 接下来实现抽象类Command(也可以使用接口),对于ObjectCommand类型来说,它自己实现所有功能,根本不需要额外的接收者对象,所以在Command中声明了所有的操作abstract class Command public Command() public abstract void Write(); public abstract void Delete(); public abstract void UnDo();有了Command,就可以实现具体的操作类型DocumentCommand(结构图中的ConcreteCommand)class DocumentCommand : Command private Document doc; private ArrayList ObjectState = new ArrayList();/Record State public DocumentCommand(Document doc) this.doc = doc; public override void Write() Console.WriteLine(Please input an character:); string strRead = Console.ReadLine(); doc.strContent += strRead; ObjectState.Add(doc.strContent); public override void Delete() doc.strContent = doc.strContent.Substring(0, doc.strContent.Length - 1); ObjectState.Add(doc.strContent); public override void UnDo() doc.strContent = (string)ObjectStateObjectState.Count - 2; ObjectState.Add(doc.strContent); 接下来就用一个客户端代码作一下测试class Program static void Main(string args) Document doc = new Document(); DocumentCommand com = new DocumentCommand(doc); Console.WriteLine(Please Input next operation:); string strOperation = Console.ReadLine(); while (strOperation != Exit) switch (strOperation.ToLower() case write: com.Write(); Console.WriteLine(Write Operation: + doc.strContent); break; case del: com.Delete(); Console.WriteLine(Delete Operation: + doc.strContent); break; case undo: com.UnDo(); Console.WriteLine(UnDo Operation: + doc.strContent); break; default: Console.WriteLine(Wrong Operation:); break; Console.WriteLine(Please Input next operation:); strOperation = Console.ReadLine(); 测试结果如下:Please Input next operation:writePlease input an character:kWrite Operation:kPlease Input next operation:writePlease input an character:iWrite Operation:kiPlease Input next operation:writePlease input an character:dWrite Operation:kidPlease Input next operation:writePlease input an character:dWrite Operation:kiddPlease Input next operation:delDelete Operation:kidPlease Input next operation:undoUnDo Operation:kiddPlease Input next operation:这两个程序中需要有几点说明:1、 对于OperationComm
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 上海招生营销方案
- 游仙区布线施工方案
- 油路桥施工方案
- 春节营销方案模版
- 丙纶地毯施工方案
- 语音发音专项训练指导方案
- 高速公路维修施工组织设计方案
- 计量检定机构复核整改方案范本
- 基础土方施工方案与技术措施
- 城市广场混凝土大体积施工方案
- 13S201室外消火栓及消防水鹤安装
- 人工智能通识 课件 第七章 智能之躯-具身智能
- 个人充电桩免责协议书
- 梯形、矩形渠道水力计算表-有用(算出流速和流量)
- 芯片设计师面试题目及答案
- 重庆学法减分试题及答案
- 2025中小学学校教材教辅征订管理工作方案
- 小学数学学习障碍干预:教师教学经验与改进路径研究
- ASME B16.10-2022 阀门结构长度(中英文参考版)
- 降低留置针堵管发生率:PDCA质量持续改进
- 违建自愿拆除协议书
评论
0/150
提交评论