在屏幕输出上改进了点,这样方便理解,也修改了一个bug,可参照原文。
Long long ago,在南方的一个异国他乡,有一个叫Peter的勤劳的工人,他对老板(boss)百依百顺,然而他的boss却是个卑鄙多疑的家伙,他坚持要求Peter不断汇报工作进展。由于Peter不希望被boss盯着干活,于是他向boss承诺随时汇报工作进度。Peter通过如下所示的类型化的引用(typed reference)定期回调boss来实现这个承诺:
class Worker {
public void Advise(Boss boss) {this.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);
}
}
Boss boss;
}
class Boss {
public void WorkStarted()
{
Console.WriteLine("Boss: i don't care!");
}
public void WorkProgressing()
{
Console.WriteLine("Boss: i don't care!");
}
public int WorkCompleted() {
Console.WriteLine("Boss: Good work ,i'll grade you!");
return 2; /* 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();
}
}
1.1 接口
现在,Peter成了一个特殊人物,他不但能够忍受卑鄙的boss,和周围的世界(universe)也建立了紧密的联系。Peter感到universe对他的工作进程同样感兴趣。不幸的是,如果不为universe添加一个特殊的Advise方法和特殊的回调,除了保证boss能够被通知外,Peter并不能向universe通知工作进度。Peter希望能从那些通知方法的实现中分离出潜在的通知列表,为此,他决定将方法分离到一个接口中:
interface IWorkerEvents {
void WorkStarted();
void WorkProgressing();
int WorkCompleted();
}
class Worker {
public void Advise(IWorkerEvents events) {this.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);
}
}
IWorkerEvents events;
}
class Boss : IWorkerEvents {
public void WorkStarted()
{
Console.WriteLine("Boss: i don't care!");
}
public void WorkProgressing()
{
Console.WriteLine("Boss: i don't care!");
}
public int WorkCompleted()
{
Console.WriteLine("Boss:Good work, i'll grade you!");
return 3; /* 10分以内 */
}
}
1.2 委托
不幸的是,由于Peter忙于说服boss实现这个接口,以至于没有顾得上通知universe也实现该接口,但他希望尽可能做到这一点,至少他已经抽象了对boss的引用,因此,别的实现了IWorkerEvents接口的什么人也可以得到工作进度通知。
然而, Peter的boss仍然极其不满。“Peter!”boss咆哮者,“你为什么要通知我什么时候开始工作、什么时候正在进行工作?我不关心这些事件,你不但强迫我实现这些方法,你还浪费了你的宝贵的工作时间等我从事件中返回。当我的实现需要占用很长时间时,你等我的时间也要大大延长!你难道不能想想别的办法不要老是来烦我吗?”
因此,Peter意识到尽管在很多情况下接口很有用,但在处理事件时,接口的粒度还不够精细。他希望能做到仅仅通知监听者真正感兴趣的事件。为此,Peter决定把接口中的方法分解为若干个独立的委托函数,每一个都好象是只包含一个方法的微型接口:
//c#默认是private
public delegate void WorkStarted();
public delegate void WorkProgressing();
public 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 WorkProgressing progressing;
public WorkCompleted completed;
}
class Boss {
public int WorkCompleted() {
Console.WriteLine("Better...");
return 4; /* 10分以内 */
}
}
class Universe {
static void Main() {
Worker peter = new Worker();
Boss boss = new Boss();
// 注意:我们已将Advise方法替换为赋值运算符
//peter 只向boss报告他关心的问题
peter.completed = new WorkCompleted(boss.WorkCompleted);
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
}
1.3 静态订阅者
利用委托,Peter达到了不拿boss不关心的事件去烦他的目标,然而Peter还是不能够使universe成为其订阅者之一。因为universe是一个全封闭的实体,所以将委托挂钩在实例成员上不妥的(设想一下Universe的多个实例需要多少资源)。相反,Peter需要将委托挂钩到静态成员上,因为委托也完全支持静态成员:
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想得到Boss ,Uninverse两个人的grade该怎么办呢?
peter.DoWork();
Console.WriteLine("Main: worker completed work");
Console.ReadLine();
}
}