Delegate就是接口,我是这样理解它的.
后来发现Delegate比起接口可以"乱用".因为它只要方法的签名一样就可以替换.比如下面这个例子.
publicclass Client ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public dalegate int AddHandle(int a, int b);
public AddHandle Add ;
public void Do() ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
Add(2,3);
}
}
publicclass Math ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
publicint Add(int a,int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a+b;
}
publicint Sub(int a,int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a-b;
}
}
这下可好,如果象下面这样用,结果岂不变成-1了
Client c=new Client();
c.Add =new Client.AddHandle(new Math().Sub);
c.Do();
如果用接口,约束性就强了点.
publicclass Client ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public AddHandle iadder;
publicvoid Do() ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
iadder.Add();
}
}
publicinterface AddHandle
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int Add(int a, int b);
}
publicclass Client ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public AddHandle Add;
}
publicclass Math : AddHandle ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
publicint Add(int a, int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a + b;
}
publicint Sub(int a, int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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 ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
publicint Add(int a,int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a+b;
}
publicint Sub(int a,int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a-b;
}
}
privateint wage=0;
publicvoid ModifyWage(int value,OperationHandle operation) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int newWage= operation(wage, value);
wage= newWage;
}
加加减减,甚至乘除,只要绑定上不同的operation就ok了,这样岂不是很方便? 如果用接口实现呢?
publicinterface OperationHandle ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int Execute(int a, int b);
}
publicclass Add : OperationHandle ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
publicint Execute(int a, int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a + b;
}
}
publicclass Sub: OperationHandle ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
publicint Execute(int a, int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a - b;
}
}
privateint wage=0;
publicvoid ModifyWage(int value,OperationHandle operation) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int newWage= operation.Execute(wage, value);
wage= newWage;
}
可以看出这就是Command模式. 还有好玩的,Delegate不是可以使用多播嘛(+=),用在这里就更好玩了.
public delegateint OperationHandle(int a, int b);
publicclass Math ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
publicint Add(int a,int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a+b;
}
publicint Sub(int a,int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return a-b;
}
}
privateint wage=0;
publicvoid ModifyWage(int [] value,OperationHandle operation) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i=0;
foreach(OperationHandle oh in operation.GetInvokeList()) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int newWage= oh(wage, value[i]);
wage+= newWage;
i++;
}
}
很方便吧,接口也能做.Command加上Composition,不过要想变换参数似乎有点困难,(这仅仅是个例子,不考虑那么多了)
publicclass Composite : OperationHandle ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
IList operations =new ArrayList();
publicint Execute(int a, int b) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int result =0;
foreach(Operation o in operations)
result += o.Execute(a, b);
return result;
}
publicvoid AddOperation(Operation o) ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
operations.Add(o);
}
}
看完这些你感觉怎么样?难怪有人说.net的架构师是Delegate先生,delegate做的事,interface
基本上都能做,不过delegate是带来了不少方便,不过初学者比较难理解delegate这个概念,而且如前文所说
delegate在带来灵活性的同时也带来了一定的危险.