委托和事件— 一个虚构的故事 |
中国自学编程网收集整理 |
<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> |
1 委托 从前,在南方的一个异国他乡,有一个叫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() {/* boss不关心 */ } public void WorkProgressing() {/* boss不关心 */ } public int WorkCompleted() { Console.WriteLine("It's about time!"); 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() {/* boss不关心 */ } public void WorkProgressing() {/* boss不关心 */ } public int WorkCompleted() { Console.WriteLine("It's about time!"); return 3; /* 10分以内 */ } } 1.2 委托 不幸的是,由于Peter忙于说服boss实现这个接口,以至于没有顾得上通知universe也实现该接口,但他希望尽可能做到这一点,至少他已经抽象了对boss的引用,因此,别的实现了IWorkerEvents接口的什么人也可以得到工作进度通知。 然而, Peter的boss仍然极其不满。“Peter!”boss咆哮者,“你为什么要通知我什么时候开始工作、什么时候正在进行工作?我不关心这些事件,你不但强迫我实现这些方法,你还浪费了你的宝贵的工作时间等我从事件中返回。当我的实现需要占用很长时间时,你等我的时间也要大大延长!你难道不能想想别的办法不要老是来烦我吗?” 因此,Peter意识到尽管在很多情况下接口很有用,但在处理事件时,接口的粒度还不够精细。他希望能做到仅仅通知监听者真正感兴趣的事件。为此,Peter决定把接口中的方法分解为若干个独立的委托函数,每一个都好象是只包含一个方法的微型接口: 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 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.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.DoWork(); Console.WriteLine("Main: worker completed work"); Console.ReadLine(); } } 2 事件 不幸的是,由于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(); } 2.1 获取所有结果 至此,Peter终于松了一口气。他已经设法满足了所有订阅者的需求,而且不会和特定实现紧密耦合。然而,他又注意到尽管boss和universe都为他的工作打了分,但他只得到了一个打分。在有多个订阅者的情形下,Peter希望能得到所有订阅者的评分结果。因此,他决定“进入委托”,提取订阅者列表,以便手工分别调用它们: public void DoWork() { ... Console.WriteLine("Worker: work completed"); if( completed != null ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { int grade = wc(); Console.WriteLine("Worker grade= " + grade); } } } 2.2 异步通知:触发和忽略 不料,在此期间,boss和universe被别的什么事纠缠上了,这就意味着他们给Peter的工作打分的时间被大大延长了: class Boss { public int WorkCompleted() { System.Threading.Thread.Sleep(3000); Console.WriteLine("Better..."); return 6; /* 10分以内 */ } } class Universe { static int WorkerCompletedWork() { System.Threading.Thread.Sleep(4000); Console.WriteLine("Universe is pleased with worker's work"); return 7; } ... } 不幸的是,由于Peter是同时通知每一个订阅者并等待他们打分的,这些需要返回评分的通知现在看来要占用他不少工作时间,因此,Peter决定忽略评分并且异步触发事件: public void DoWork() { ... Console.WriteLine("Worker: work completed"); if( completed != null ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { wc.BeginInvoke(null, null); } } } 2.3 异步通知:轮询 这个聪明的小把戏允许Peter在通知订阅者的同时能立即返回工作,让进程的线程池调用委托。然而没过多久Peter就发现订阅者给他的打分被搞丢了。他知道自己工作做得不错,并乐意universe作为一个整体(而不仅仅是他的boss)表扬他。因此,Peter异步触发事件,但定期轮询,以便察看可以获得的评分: public void DoWork() { ... Console.WriteLine("Worker: work completed"); if( completed != null ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { IAsyncResult res = wc.BeginInvoke(null, null); while( !res.IsCompleted ) System.Threading.Thread.Sleep(1); int grade = wc.EndInvoke(res); Console.WriteLine("Worker grade= " + grade); } } } 2.4 异步通知:委托 不幸的是,Peter又回到了问题的起点,就像他一开始希望避免boss站在一旁边监视他工作一样。因此,Peter决定使用另一个委托作为异步工作完成时的通知方式,这样他就可以立即回去工作,而当工作被打分时,仍然可以接到通知: public void DoWork() { ... Console.WriteLine("Worker: work completed"); if( completed != null ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { wc.BeginInvoke(new AsyncCallback(WorkGraded), wc); } } } void WorkGraded(IAsyncResult res) { WorkCompleted wc = (WorkCompleted)res.AsyncState; int grade = wc.EndInvoke(res); Console.WriteLine("Worker grade= " + grade); } 3 普天同乐 Peter、boss和universe最终都满意了。boss和universe都可以仅被通知其感兴趣的事件,并减少了实现的负担和不必要的来回调用。Peter可以通知他们每一个人,而不必管需要多长时间才能从那些目标方法中返回,并仍然可以异步得到评分结果。结果得到如下完整的解决方案: 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 ) { foreach( WorkCompleted wc in completed.GetInvocationList() ) { wc.BeginInvoke(new AsyncCallback(WorkGraded), wc); } } } void WorkGraded(IAsyncResult res) { WorkCompleted wc = (WorkCompleted)res.AsyncState; int grade = wc.EndInvoke(res); Console.WriteLine("Worker grade= " + grade); } public event WorkStarted started; public event WorkProgressing progressing; public event WorkCompleted completed; } class Boss { public int WorkCompleted() { System.Threading.Thread.Sleep(3000); Console.WriteLine("Better..."); return 6; /* 10分以内 */ } } class Universe { static void WorkerStartedWork() { Console.WriteLine("Universe notices worker starting work"); } static int WorkerCompletedWork() { System.Threading.Thread.Sleep(4000); Console.WriteLine("Universe is 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(); } } Peter知道异步获取结果会带来一些问题。由于异步触发事件,所以目标方法有可能执行于另一个线程中,就像Peter的“目标方法何时完成”的通知那样。然而,Peter熟悉第14章“多线程用户界面”,因此,他知道在构建WinForms应用程序时如何去处理此类问题。 从此,他们一直过得都很快乐。 |
11-15
11-15
11-15
11-15