Delegate和Command Pattern
Delegate就是接口,我是这样理解它的.
后来发现Delegate比起接口可以"乱用".因为它只要方法的签名一样就可以替换.比如下面这个例子.
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了
c.Do();
如果用接口,约束性就强了点.
{ 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的这个灵活的特点,我想了一个合理的应用---计算工资.
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了,这样岂不是很方便? 如果用接口实现呢?
可以看出这就是Command模式. 还有好玩的,Delegate不是可以使用多播嘛(+=),用在这里就更好玩了.
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,不过要想变换参数似乎有点困难,(这仅仅是个例子,不考虑那么多了)
result += o.Execute(a, b); return result; } publicvoid AddOperation(Operation o) { operations.Add(o); } }
看完这些你感觉怎么样?难怪有人说.net的架构师是Delegate先生,delegate做的事,interface
基本上都能做,不过delegate是带来了不少方便,不过初学者比较难理解delegate这个概念,而且如前文所说
delegate在带来灵活性的同时也带来了一定的危险.
Delegate和Command Pattern相关推荐
- command pattern
1.定义(http://en.wikipedia.org/wiki/Command_pattern#Java) In object-oriented programming, the command ...
- 设计模式系列3-----C++实现命令模式(Command Pattern)
什么是命令模式? GoF的书的定义为:"Command pattern encapsulate request as an object, thereby letting you param ...
- 设计模式:命令模式(Command Pattern)
命令模式(Command Pattern): 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接受者是谁,也不知道请求的操作是哪个. 我们只需在程序运行时指定具体的请求接受者即可,此时 ...
- 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释
命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...
- 设计模式之-命令模式(Command Pattern)
命令模式(Command Pattern)是用来实现在一个请求 - 响应模型松耦合.在命令模式中,请求被发送给调用者和调用它传递给被封装的命令对象. Command对象将请求传递到接收器的适当的方法来 ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
- 行为型设计模式(3)—— 命令模式(Command Pattern)
文章目录 1.概述 2.命令模式简单实现 3.命令模式的应用场景和优缺点 4.小结 参考文献 1.概述 使用设计模式可以提高代码的可复用性.可扩充性和可维护性.命令模式(Command Pattern ...
- Command Pattern的简单介绍
Command pattern 的角色有:Command(抽象命令).ConcreteCommand(具体命令) .Invoker(传达命令者) .receiver(接收命令者)Client(客户类, ...
- 秒懂设计模式之命令模式(Command Pattern)
[版权申明] 非商业目的注明出处可自由转载 博文地址:https://blog.csdn.net/ShuSheng0007/article/details/116115743 出自:shusheng0 ...
最新文章
- linux yum 本地源配置
- 基于mysql 5.5+mysql-master-ha实现mysql ha架构
- SpringBoot用Servlet处理请求
- C++类的使用(一)
- java 8和jdk区别_java-8 – JDK 6和JDK8之间的Java Collection差异
- 微信打击违规贷款、仿冒公众号等行为 累积处罚3万多个公众号
- 九度搜索引擎点击优化_深圳坂田企业老总在想竞价推广和网站优化选哪个?
- golang操作mongodb的驱动mongo-go-driver的事务支持和访问控制(mongodb4.0)
- iOS开发者必备:五大编程类工具
- 华为2019年3月软件开发工程师机考题解答 C++实现
- Android命令-重点命令-pm/am/content/wm/appops
- SpringBoot 集成 微信绑定 微信登录
- Android_应用程序权限
- 惠普笔记本连接wifi无internet,手机连接正常
- 【JavaEE】图书管理系统-简易版
- 阿里云的PLOARDB
- iOS开发之苹果公司联系邮箱大全
- 区别MSE,欧式距离的公式
- 不胜人生一场醉入住新家和感言!
- 智慧机场:如何基于EasyCVR建设机场周界入侵安防系统?