Delegate就是接口,我是这样理解它的.
后来发现Delegate比起接口可以"乱用".因为它只要方法的签名一样就可以替换.比如下面这个例子.

publicclass Client       public  dalegate int AddHandle(int a, int b);       public  AddHandle Add ;          
     public  void Do()      {        //        Add(2,3);      } publicclass Math      publicint Add(int a,int b)                return a+b;      }            publicint Sub(int a,int b)                return a-b;      }      }

这下可好,如果象下面这样用,结果岂不变成-1了

Client c=new Client();  c.Add =new Client.AddHandle(new Math().Sub); 
c.Do();

如果用接口,约束性就强了点.

publicclass Client {     public AddHandle iadder;         publicvoid Do()         {            //            iadder.Add();         } } publicinterface AddHandle
{     int Add(int a, int b); } publicclass Client {     public AddHandle Add; } publicclass Math : AddHandle {     publicint Add(int a, int b)     {         return a + b;     }     publicint Sub(int a, int b)     {         return a - b;     } } Client c=new Client(); 
c.iadder=new Math(); c.Do();

这样不太可能出上面那样的错误. 不过Delegate这么灵活也不全是错.
如果Math中的方法是Static的(也是很有可能的),那接口就傻眼了,但是Delegate照样能搞定.
根据Delegate的这个灵活的特点,我想了一个合理的应用---计算工资.

public  delegatedouble OperationHandle(double a, double b);
publicclass Math {     publicint Add(int a,int b)     {           return a+b;     }          publicint Sub(int a,int b)     {           return a-b;     } } privateint wage=0; publicvoid ModifyWage(int  value,OperationHandle operation) { int newWage= operation(wage, value); wage= newWage; }

加加减减,甚至乘除,只要绑定上不同的operation就ok了,这样岂不是很方便? 如果用接口实现呢?

publicinterface OperationHandle { int Execute(int a, int b); } publicclass Add : OperationHandle { publicint Execute(int a, int b) {   return a + b; } } publicclass Sub: OperationHandle { publicint Execute(int a, int b) {   return a - b; } } privateint  wage=0; publicvoid ModifyWage(int value,OperationHandle operation) { int  newWage= operation.Execute(wage, value); wage= newWage; }

可以看出这就是Command模式. 还有好玩的,Delegate不是可以使用多播嘛(+=),用在这里就更好玩了.

public  delegateint OperationHandle(int a, int b);
publicclass Math {     publicint Add(int a,int b)     {           return a+b;     }          publicint Sub(int a,int b)     {           return a-b;     } } privateint wage=0; publicvoid ModifyWage(int [] value,OperationHandle operation) {                 int i=0;                 foreach(OperationHandle oh in operation.GetInvokeList())                 {         int newWage= oh(wage, value[i]);         wage+= newWage;                                    i++;                 } }

很方便吧,接口也能做.Command加上Composition,不过要想变换参数似乎有点困难,(这仅仅是个例子,不考虑那么多了)

publicclass Composite : OperationHandle {     IList operations =new ArrayList();     publicint Execute(int a, int b)     {         int result =0;         foreach(Operation o in operations)
            result += o.Execute(a, b);         return result;     }     publicvoid AddOperation(Operation o)     {         operations.Add(o);     } }

看完这些你感觉怎么样?难怪有人说.net的架构师是Delegate先生,delegate做的事,interface
基本上都能做,不过delegate是带来了不少方便,不过初学者比较难理解delegate这个概念,而且如前文所说
delegate在带来灵活性的同时也带来了一定的危险.

Delegate和Command Pattern相关推荐

  1. command pattern

    1.定义(http://en.wikipedia.org/wiki/Command_pattern#Java) In object-oriented programming, the command ...

  2. 设计模式系列3-----C++实现命令模式(Command Pattern)

    什么是命令模式? GoF的书的定义为:"Command pattern encapsulate request as an object, thereby letting you param ...

  3. 设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern): 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接受者是谁,也不知道请求的操作是哪个. 我们只需在程序运行时指定具体的请求接受者即可,此时 ...

  4. 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释

    命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...

  5. 设计模式之-命令模式(Command Pattern)

    命令模式(Command Pattern)是用来实现在一个请求 - 响应模型松耦合.在命令模式中,请求被发送给调用者和调用它传递给被封装的命令对象. Command对象将请求传递到接收器的适当的方法来 ...

  6. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  7. 行为型设计模式(3)—— 命令模式(Command Pattern)

    文章目录 1.概述 2.命令模式简单实现 3.命令模式的应用场景和优缺点 4.小结 参考文献 1.概述 使用设计模式可以提高代码的可复用性.可扩充性和可维护性.命令模式(Command Pattern ...

  8. Command Pattern的简单介绍

    Command pattern 的角色有:Command(抽象命令).ConcreteCommand(具体命令) .Invoker(传达命令者) .receiver(接收命令者)Client(客户类, ...

  9. 秒懂设计模式之命令模式(Command Pattern)

    [版权申明] 非商业目的注明出处可自由转载 博文地址:https://blog.csdn.net/ShuSheng0007/article/details/116115743 出自:shusheng0 ...

最新文章

  1. linux yum 本地源配置
  2. 基于mysql 5.5+mysql-master-ha实现mysql ha架构
  3. SpringBoot用Servlet处理请求
  4. C++类的使用(一)
  5. java 8和jdk区别_java-8 – JDK 6和JDK8之间的Java Collection差异
  6. 微信打击违规贷款、仿冒公众号等行为 累积处罚3万多个公众号
  7. 九度搜索引擎点击优化_深圳坂田企业老总在想竞价推广和网站优化选哪个?
  8. golang操作mongodb的驱动mongo-go-driver的事务支持和访问控制(mongodb4.0)
  9. iOS开发者必备:五大编程类工具
  10. 华为2019年3月软件开发工程师机考题解答 C++实现
  11. Android命令-重点命令-pm/am/content/wm/appops
  12. SpringBoot 集成 微信绑定 微信登录
  13. Android_应用程序权限
  14. 惠普笔记本连接wifi无internet,手机连接正常
  15. 【JavaEE】图书管理系统-简易版
  16. 阿里云的PLOARDB
  17. iOS开发之苹果公司联系邮箱大全
  18. 区别MSE,欧式距离的公式
  19. 不胜人生一场醉入住新家和感言!
  20. 智慧机场:如何基于EasyCVR建设机场周界入侵安防系统?

热门文章

  1. ONES 万事联合创始人 amp; CTO 冯斌:企业服务产品的探索实践
  2. Realm发布Realm .NET,扩展支持.NET技术栈
  3. 超级实用的linux 下shell快捷键汇总
  4. “AV终结者/8749”病毒清理办法
  5. CSS过渡动画的理解
  6. 从一道常见习题的自然延伸谈起
  7. 科普:5G网络关键技术详解
  8. 重温ES6核心概念和基本用法
  9. 读书笔记之《习惯的力量》
  10. [置顶] 程序员的奋斗史(十五)——谈性格