观察者模式
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using System.Collections;
7
8namespace ConsoleApplication2
9{
10 public class Program
11 {
12 /**//// <summary>
13 /// 被观察者接口
14 /// </summary>
15 public interface IObserver
16 {
17 void Notify();
18 }
19 /**//// <summary>
20 /// 观察者接口
21 /// </summary>
22 public interface IObservable
23 {
24 void Register(IObserver observer);
25 void UnRegister(IObserver observer);
26 void Notify();
27 }
28 /**//// <summary>
29 /// 被观察基类
30 /// </summary>
31 public abstract class Observable : IObservable
32 {
33 private List<IObserver> _lists = new List<IObserver>();
34
35 IObservable 成员#region IObservable 成员
36
37 public void Register(IObserver observer)
38 {
39 this._lists.Add(observer);
40 }
41
42 public void UnRegister(IObserver observer)
43 {
44 this._lists.Remove(observer);
45 }
46
47 public void Notify()
48 {
49 foreach (IObserver observer in this._lists)
50 {
51 observer.Notify();
52 }
53 }
54
55 #endregion
56 }
57 /**//// <summary>
58 /// 观察者1
59 /// </summary>
60 public class SendMail : IObserver
61 {
62 IObserver 成员#region IObserver 成员
63
64 public void Notify()
65 {
66 Console.WriteLine("发送邮件!");
67 }
68
69 #endregion
70 }
71 /**//// <summary>
72 /// 观察者2
73 /// </summary>
74 public class SendFile : IObserver
75 {
76 IObserver 成员#region IObserver 成员
77
78 public void Notify()
79 {
80 Console.WriteLine("发送文件!");
81 }
82
83 #endregion
84 }
85 /**//// <summary>
86 /// 被观察对象
87 /// </summary>
88 public class Subject : Observable
89 {
90 public void Send()
91 {
92 base.Notify();
93 }
94 }
95
96 public static void Main()
97 {
98 Subject subject = new Subject();
99
100 SendMail mail = new SendMail();
101 SendFile file = new SendFile();
102 subject.Register(mail);
103 subject.Register(file);
104
105 subject.Send();
106 }
107 }
108}
109
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using System.Collections;
7
8namespace ConsoleApplication2
9{
10 public class Program
11 {
12 /**//// <summary>
13 /// 被观察者接口
14 /// </summary>
15 public interface IObserver
16 {
17 void Notify();
18 }
19 /**//// <summary>
20 /// 观察者接口
21 /// </summary>
22 public interface IObservable
23 {
24 void Register(IObserver observer);
25 void UnRegister(IObserver observer);
26 void Notify();
27 }
28 /**//// <summary>
29 /// 被观察基类
30 /// </summary>
31 public abstract class Observable : IObservable
32 {
33 private List<IObserver> _lists = new List<IObserver>();
34
35 IObservable 成员#region IObservable 成员
36
37 public void Register(IObserver observer)
38 {
39 this._lists.Add(observer);
40 }
41
42 public void UnRegister(IObserver observer)
43 {
44 this._lists.Remove(observer);
45 }
46
47 public void Notify()
48 {
49 foreach (IObserver observer in this._lists)
50 {
51 observer.Notify();
52 }
53 }
54
55 #endregion
56 }
57 /**//// <summary>
58 /// 观察者1
59 /// </summary>
60 public class SendMail : IObserver
61 {
62 IObserver 成员#region IObserver 成员
63
64 public void Notify()
65 {
66 Console.WriteLine("发送邮件!");
67 }
68
69 #endregion
70 }
71 /**//// <summary>
72 /// 观察者2
73 /// </summary>
74 public class SendFile : IObserver
75 {
76 IObserver 成员#region IObserver 成员
77
78 public void Notify()
79 {
80 Console.WriteLine("发送文件!");
81 }
82
83 #endregion
84 }
85 /**//// <summary>
86 /// 被观察对象
87 /// </summary>
88 public class Subject : Observable
89 {
90 public void Send()
91 {
92 base.Notify();
93 }
94 }
95
96 public static void Main()
97 {
98 Subject subject = new Subject();
99
100 SendMail mail = new SendMail();
101 SendFile file = new SendFile();
102 subject.Register(mail);
103 subject.Register(file);
104
105 subject.Send();
106 }
107 }
108}
109