发布订阅者模式之C#委托实现

1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6
  7 namespace 委托与发布订阅者模式
  8 {
  9
10     /**
11      * 机器Machine为被订阅者,维修工Repairer为订阅者
12      * 工人(订阅者)注册机器的不同事件,并注册回调函数以当相应事件发生时进行处理
13      * 不同的工人可以只关注他们各自感兴趣的事件
14      */
15     // 机器类
16     public class Machine
17     {
18         // 机器是否正常工作状态
19         public bool IsWorkWell { get; set; }
20
21         // 机器故障事件委托
22         public delegate void MachineBroken(string brokenMsg);
23         public event MachineBroken MachineBrokenEvent;
24
25         // 机器正常工作事件委托
26         public delegate void MachineWorkWell(string msg);
27         public event MachineWorkWell MachineWorkWellEvent;
28
29         // 机器运作
30         public void Run()
31         {
32             IsWorkWell = false; // 设置机器出故障
33
34             if (MachineBrokenEvent != null) // 有订阅者注册机器故障事件
35             {
36                 if (!IsWorkWell)
37                     MachineBrokenEvent("机器出故障了,速度过来看下啊,亲~");
38             }
39
40             IsWorkWell = true; // 设置机器恢复正常
41
42             if (MachineWorkWellEvent != null) // 有订阅者注册机器运行正常事件
43             {
44                 if (IsWorkWell)
45                     MachineWorkWellEvent("机器正常运作,不用担心,亲~");
46             }
47         }
48
49         //public void OnMachineBrokenEvent(string brokenmsg)
50         //{
51         //    MachineBroken handler = machineBrokenEvent;
52         //    if (handler != null) handler(brokenmsg);
53         //}
54
55         //public void OnMachineWorkWellEvent(string msg)
56         //{
57         // MachineWorkWell handler = machineWorkWellEvent; 58         //    if (handler != null) handler(msg);
59         //}
60     }
61
62     public interface IWorker
63     {
64         string Name { get; set; }
65         void DoJob(string msg);
66     }
67
68     // BusyWorker注册机器故障事件
69     public class BusyWorker: IWorker
70     {
71         public string Name { get; set; }
72
73         public void DoJob(string msg)
74         {
75             Console.WriteLine(string.Format("{0}: 收到信息 < {1} > !马上过来处理,稍等啊~", Name, msg));
76         }
77     }
78
79     // IdleWorker注册机器正常工作事件
80     public class IdleWorker: IWorker
81     {
82         public string Name { get; set; }
83
84         public void DoJob(string msg)
85         {
86          Console.WriteLine(string.Format("{0}: 收到信息 < {1} > !非常好,这样我就可以多睡会了~", Name, msg));  
87         }
88     }
89
90     public class SubscriberPatternViaDelegate
91     {
92         static void Main(string[] args)
93         {
94             Machine machine = new Machine();
95
96             IWorker busyWorker = new BusyWorker();
97             busyWorker.Name = "Busy Worker";
98
99             IWorker idleWorker = new IdleWorker();
100             idleWorker.Name = "Idle Worker";
101
102             machine.MachineBrokenEvent += new Machine.MachineBroken(busyWorker.DoJob);
103             machine.MachineWorkWellEvent += new Machine.MachineWorkWell(idleWorker.DoJob);
104
105             ///////////////////////////////////////////////////////////////////////////////////////////
106             // 下面开始验证是否可以为一个事件添加多个订阅者
107             IWorker busyWorker2 = new BusyWorker();
108             busyWorker2.Name = "Busy Worker 2";
109             machine.MachineBrokenEvent += new Machine.MachineBroken(busyWorker2.DoJob);
110             // 试验结果是可以的
111             // 同样可以使用静态委托,静态委托的好处之一是节省了内存空间
112             ///////////////////////////////////////////////////////////////////////////////////////////
113
114             machine.Run();  // 机器开始运作
115
116             Console.ReadLine();
117         }
118     }
119 }

posted @ 2014-03-13 11:05  armyfai  阅读(2447)  评论(2编辑  收藏  举报