.NET Delegates: A C# Bedtime Story中文版(上篇)
作者:Chris Sells
译者:荣耀
【译注:C#进阶文章。Chris Sells是《ATL Internals》一书作者之一。译文中所有程序调试环境均为Microsoft Visual Studio.NET 7.0 Beta2和 Microsoft .NET Framework SDK Beta2。代码就是文章,请仔细阅读代码J】
类型耦合
从前,在南方的一个异国他乡,有一个叫peter的勤劳的工人。他对boss百依百顺,但他的boss却是个卑鄙无信的小人,他坚持要求peter不断汇报工作情况。由于peter不希望被boss盯着干活,于是他向boss承诺随时汇报工作进度。peter利用类型引用定期回调boss来实现这个承诺:
using System;//【译注:译者补充】
class Worker
{
public void Advise(Boss boss)
{
_boss = boss;
}
public void DoWork()
{
Console.WriteLine("Worker: work started");
if( _boss != null ) _boss.WorkStarted();
Console.WriteLine("Worker: work progressing");
if( _boss != null ) _boss.WorkProgressing();
Console.WriteLine("Worker: work completed");
if( _boss != null )
{
int grade = _boss.WorkCompleted();
Console.WriteLine("Worker grade = " + grade);
}
}
private Boss _boss;
}
class Boss
{
public void WorkStarted() { /*boss不关心. */ }
public void WorkProgressing() { /*boss不关心. */ }
public int WorkCompleted()
{
Console.WriteLine("It's about time!");
return 2; /* out of 10 */
}
}
class Universe
{
static void Main()
{
Worker peter = new Worker();
Boss boss = new Boss();
peter.Advise(boss);
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
}
/*【译注:以下是上段程序输出结果:
Worker: work started
Worker: work progressing
Worker: work completed
It's about time!
Worker grade = 2
Main: worker completed work
】*/
接口
现在,peter成了一个特殊人物,他不但能够忍受卑鄙的boss,和universe也建立了紧密的联系。peter感到universe对他的工作进程同样感兴趣。不幸的是,除了保证boss能够被通知外,如果不为universe添加一个特殊的通知方法和回调,peter无法向universe通知其工作进程。Peter希望能从那些通知方法的实现中分离出潜在的通知约定,为此,他决定将方法剥离到接口中:
using System; //【译注:译者补充】
interface IWorkerEvents //【译注:这就是分离出来的接口】
{
void WorkStarted();
void WorkProgressing();
int WorkCompleted();
}
class Worker
{
public void Advise(IWorkerEvents events) //【译注:现在传递的参数类型为接口引用】
{
_events = events;
}
public void DoWork()
{
Console.WriteLine("Worker: work started");
if( _events != null ) _events.WorkStarted();
Console.WriteLine("Worker: work progressing");
if(_events != null ) _events.WorkProgressing();
Console.WriteLine("Worker: work completed");
if(_events != null )
{
int grade = _events.WorkCompleted();
Console.WriteLine("Worker grade = " + grade);
}
}
private IWorkerEvents _events;
}
class Boss : IWorkerEvents //【译注:Boss实现该接口】
{
public void WorkStarted(){ /*boss不关心. */ }
public void WorkProgressing(){ /*boss不关心. */ }
public int WorkCompleted()
{
Console.WriteLine("It's about time!");
return 3; /* out of 10 */
}
}
class Universe
{
static void Main()
{
Worker peter = new Worker();
Boss boss = new Boss();
peter.Advise(boss); //【译注:或peter.Advise((IWorkerEvents)boss);】
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
}
/*【译注:以下是上段程序输出结果:
Worker: work started
Worker: work progressing
Worker: work completed
It's about time!
Worker grade = 3
Main: worker completed work
】*/
委托
不幸的是,由于peter忙于通知boss实现这个接口,以至于没有顾得上通知universe也实现该接口,但他知道不久就需如此,至少,他已经抽象了对boss的引用,因此,别的实现了IworkerEvents接口的什么人都可以收到工作进度通知。【译注:请参见上一节代码示例及译注】
然而,peter的boss依然极度不满,“Peter!”boss咆哮者,“你为什么要通知我什么时候开始工作、什么时候正在进行工作?我不关心这些事件,你不但强迫我实现这些方法,你还浪费了你的宝贵的工作时间等我从事件中返回。当我实现的方法需占用很长时间时,你等我的时间也要大大延长!你难道不能想想别的办法不要老是来烦我吗?”
此时,peter意识到尽管在很多情况下接口很有用,但在处理事件时,接口的粒度还不够精细。他还要能做到仅仅通知监听者真正感兴趣的事件。因此,peter决定把接口里的方法肢解成若干个独立的委托函数,每一个都好象是只有一个方法的小接口。
using System; //【译注:译者补充】
delegate void WorkStarted();
delegate void WorkProgressing();
delegate int WorkCompleted();
class Worker
{
public void DoWork()
{
Console.WriteLine("Worker: work started");
if( started != null ) started();
Console.WriteLine("Worker: work progressing");
if( progressing != null ) progressing();
Console.WriteLine("Worker: work completed");
if( completed != null )
{
int grade = completed();
Console.WriteLine("Worker grade = " + grade);
}
}
public WorkStarted started; //【译注:这样写更规矩:public WorkStarted started = null;】
public WorkProgressing progressing; //【译注:这样写更规矩:public WorkProgressing progressing = null;】
public WorkCompleted completed; //【译注:这样写更规矩:public WorkCompleted completed = null;】
}
class Boss
{
public int WorkCompleted()
{
Console.WriteLine("Better...");
return 4; /* out of 10 */
}
}
class Universe
{
static void Main()
{
Worker peter = new Worker();
Boss boss = new Boss();
peter.completed = new WorkCompleted(boss.WorkCompleted);
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
}
/*【译注:以下是上段程序输出结果:
Worker: work started
Worker: work progressing
Worker: work completed
Better...
Worker grade = 4
Main: worker completed work
】
*/
【译注:对“但在处理事件时,接口的粒度还不够精细”的理解可用下例说明,请仔细观察一下程序,思考一下这样做的不利之处J
using System;
interface IWorkStartedEvent
{
void WorkStarted();
}
interface IWorkProgressingEvent
{
void WorkProgressing();
}
interface IWorkCompletedEvent
{
int WorkCompleted();
}
class Worker
{
public void Advise(IWorkCompletedEvent AEvent)
{
_event = AEvent;
}
public void DoWork()
{
Console.WriteLine("Worker: work completed");
if(_event != null )
{
int grade = _event.WorkCompleted();
Console.WriteLine("Worker grade = " + grade);
}
}
private IWorkCompletedEvent _event;
}
class Boss : IWorkCompletedEvent
{
public int WorkCompleted()
{
Console.WriteLine("Better...");
return 4; /* out of 10 */
}
}
class Universe
{
static void Main()
{
Worker peter = new Worker();
Boss boss = new Boss();
peter.Advise(boss);
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
}
/*以下是上段程序输出结果:
Worker: work completed
Better...
Worker grade = 4
Main: worker completed work
*/
】
静态监听者
这就达到了不用boss不关心的事件去烦他的目标。但是,peter还是不能够使universe成为其监听者。因为universe是一个全封闭的实体,所以将委托挂钩在universe的实例上不妥的(设想一下Universe的多个实例需要多少资源...)。peter意识到应将委托挂钩于universe的静态成员上,因为委托也完全适应于静态成员:
using System;
delegate void WorkStarted();
delegate void WorkProgressing();
delegate int WorkCompleted();
class Worker
{
public void DoWork()
{
Console.WriteLine("Worker: work started");
if( started != null ) started();
Console.WriteLine("Worker: work progressing");
if( progressing != null ) progressing();
Console.WriteLine("Worker: work completed");
if( completed != null )
{
int grade = completed();
Console.WriteLine("Worker grade= " + grade);
}
}
public WorkStarted started = null;
public WorkProgressing progressing = null;
public WorkCompleted completed = null;
}
class Boss
{
public int WorkCompleted()
{
Console.WriteLine("Better...");
return 4; /* out of 10 */
}
}
//【译注:以上代码为译者补充】
class Universe
{
static void WorkerStartedWork()
{
Console.WriteLine("Universe notices worker starting work");
}
static int WorkerCompletedWork()
{
Console.WriteLine("Universe pleased with worker's work");
return 7;
}
static void Main()
{
Worker peter = new Worker();
Boss boss = new Boss();
peter.completed = new WorkCompleted(boss.WorkCompleted); //【译注:×】
peter.started = new WorkStarted(Universe.WorkerStartedWork);
peter.completed = new WorkCompleted(Universe.WorkerCompletedWork);//【译注:这一行代码使得“×”那一行代码白做了L】
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
}
/*【译注:以下是上段程序输出结果:
Worker: work started
Universe notices worker starting work
Worker: work progressing
Worker: work completed
Universe pleased with worker's work
Worker grade = 7
Main: worker completed work
】*/
事件
不幸的是,universe现在变得太忙并且不习惯于注意某一个人—universe用自己的委托取代了peter的boss的委托,这显然是将Worker类的委托字段设为public的意外的副作用。【译注:请参见上节例子代码及译注】同样地,如果peter的boss不耐烦了,他自己就可以触发peter的委托(peter的boss可是有暴力倾向的)
// peter的boss自己动手了
if( peter.completed != null ) peter.completed();
peter希望确保不会发生这两种情况。他意识到必须为每一个委托加入注册和反注册函数,这样监听者就可以添加或移去它们,但谁都不能够清空整个事件列表。peter自己没去实现这些方法,相反,他使用event关键字让C#编译器帮他达到这个目的:
class Worker
{
//...
public event WorkStarted started;
public event WorkProgressing progressing;
public event WorkCompleted completed;
}
peter懂得关键字event使得委托具有这样的特性:只允许C#客户用+=或-=操作符添加或移去它们自己,这样就迫使boss和universe举止文雅一些:
static void Main()
{
Worker peter = new Worker();
Boss boss = new Boss();
peter.completed += new WorkCompleted(boss.WorkCompleted);
peter.started += new WorkStarted(Universe.WorkerStartedWork);
peter.completed += new WorkCompleted(Universe.WorkerCompletedWork);
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
【译注:以下是完整代码:
using System;
delegate void WorkStarted();
delegate void WorkProgressing();
delegate int WorkCompleted();
class Worker
{
public void DoWork()
{
Console.WriteLine("Worker: work started");
if( started != null ) started();
Console.WriteLine("Worker: work progressing");
if( progressing != null ) progressing();
Console.WriteLine("Worker: work completed");
if( completed != null )
{
int grade = completed();
Console.WriteLine("Worker grade = " + grade);
}
}
public event WorkStarted started ;
public event WorkProgressing progressing;
public event WorkCompleted completed;
}
class Boss
{
public int WorkCompleted()
{
Console.WriteLine("Better...");
return 4; /* out of 10 */
}
}
class Universe
{
static void WorkerStartedWork()
{
Console.WriteLine("Universe notices worker starting work");
}
static int WorkerCompletedWork()
{
Console.WriteLine("Universe pleased with worker's work");
return 7;
}
static void Main()
{
Worker peter = new Worker();
Boss boss = new Boss();
peter.completed += new WorkCompleted(boss.WorkCompleted); //【译注:√】
peter.started += new WorkStarted(Universe.WorkerStartedWork);
peter.completed += new WorkCompleted(Universe.WorkerCompletedWork);
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
}
/*
以下是上段程序输出结果:
Worker: work started
Universe notices worker starting work
Worker: work progressing
Worker: work completed
Better...// 【译注:boss也通知到啦J“√”那一行代码有用啦J,但是且慢,boss打的那4分没有得到,后面只得到了Universe给的7分L】
Universe pleased with worker's work
Worker grade = 7
Main: worker completed work
*/
】