博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delegate和Command Pattern
阅读量:6968 次
发布时间:2019-06-27

本文共 2557 字,大约阅读时间需要 8 分钟。

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

 

public
class
Client 
     public  dalegate int AddHandle(int a, int b);       public  AddHandle Add ;          
    
public  void Do()      {        //        Add(2,3);      } }
 
public
class
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();

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

public
class
Client
{     public AddHandle iadder;         publicvoid Do()         {            //            iadder.Add();         } }
public
interface
AddHandle
{     int Add(int a, int b); }
public
class
Client
{     public AddHandle Add; }
public
class
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
 
delegate
double
OperationHandle(
double
a,
double
b);
public
class
Math
{     publicint Add(int a,int b)     {           return a+b;     }          publicint Sub(int a,int b)     {           return a-b;     } }
private
int
wage
=
0
;
public
void
ModifyWage(
int
  value,OperationHandle operation)
{ int newWage= operation(wage, value); wage= newWage; }

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

public
interface
OperationHandle
{ int Execute(int a, int b); }
public
class
Add : OperationHandle
{ publicint Execute(int a, int b) {   return a + b; } }
public
class
Sub: OperationHandle
{ publicint Execute(int a, int b) {   return a - b; } }
private
int
  wage
=
0
;
public
void
ModifyWage(
int
value,OperationHandle operation)
{ int  newWage= operation.Execute(wage, value); wage= newWage; }

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

public
 
delegate
int
OperationHandle(
int
a,
int
b);
public
class
Math
{     publicint Add(int a,int b)     {           return a+b;     }          publicint Sub(int a,int b)     {           return a-b;     } }
private
int
wage
=
0
;
public
void
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,不过要想变换参数似乎有点困难,(这仅仅是个例子,不考虑那么多了)

public
class
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在带来灵活性的同时也带来了一定的危险.

你可能感兴趣的文章
spring与spring MVC的区别
查看>>
活动目录的综合应用(二)
查看>>
AD组策略的那点事
查看>>
CentOS 7 编译安装LAMP
查看>>
iptables
查看>>
Linux native AIO与eventfd、epoll的结合使用
查看>>
转:图数据库Neo4J的介绍
查看>>
获取屏幕宽度、浏览器宽度、网页高度,宽度信息
查看>>
我的友情链接
查看>>
Spring Boot 使用Cors (解决跨域问题)
查看>>
Android添加编译过的class文件
查看>>
VCD分解
查看>>
Android性能优化
查看>>
ibatisnet使用心得
查看>>
Python3.x 操作Mongodb
查看>>
Zookeeper集群搭建(简单)(29)
查看>>
初识Comet技术
查看>>
linux tar命令简介
查看>>
利用nginx内置ngx_http_mirror_module模块实现流量复制及流量放大
查看>>
Qt5和OpenGL一概述
查看>>