1 // 委托充当订阅者接口类
2 public delegate void NotifyEventHandler(object sender);
3
4 // 抽象订阅号类
5 public class TenXun
6 {
7 public NotifyEventHandler NotifyEvent;
8
9 public string Symbol { get; set; }
10 public string Info { get; set; }
11 public TenXun(string symbol, string info)
12 {
13 this.Symbol = symbol;
14 this.Info = info;
15 }
16
17 // 新增对订阅号列表的维护操作
18 public void AddObserver(NotifyEventHandler ob)
19 {
20 NotifyEvent += ob;
21 }
22 // 删除对订阅号列表的维护操作
23 public void RemoveObserver(NotifyEventHandler ob)
24 {
25 NotifyEvent -= ob;
26 }
27 // 推送消息
28 public void Update()
29 {
30 if (NotifyEvent != null)
31 {
32 NotifyEvent(this);
33 }
34 }
35 }
36
37 // 具体订阅号类
38 public class TenXunGame : TenXun
39 {
40 public TenXunGame(string symbol, string info)
41 : base(symbol, info)
42 {
43 }
44 }
45
46 // 具体订阅者类
47 public class Subscriber
48 {
49 public string Name { get; set; }
50 public Subscriber(string name)
51 {
52 this.Name = name;
53 }
54
55 public void ReceiveAndPrint(Object obj)
56 {
57 TenXun tenxun = obj as TenXun;
58
59 if (tenxun != null)
60 {
61 Console.WriteLine("Notified {0} of {1}'s" + " Info is: {2}", Name, tenxun.Symbol, tenxun.Inf