观察者模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
abstract class subject
{
private IList<Observer> observers = new List<Observer>();
public void Attach(Observer observer)
{
observers.Add(observer);
}
public void Detach(Observer observer)
{
observers.Remove(observer);
}
public void Notify()
{
foreach(Observer o in observers)
o.Update();
}
}
1 abstract class Observer
2 {
3 public abstract void Update();
4 }
5
1 class ConcreteSubject : Subject
2 {
3 private string subjectState;
4
5 public string SubjectState
6 {
7 get {return subjectState;}
8 set {subjectState = value;}
9 }
10 }
11
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class ConcreteObserver : Observer
2 {
3 private string name_;
4 private string observerState_;
5 private ConcreteSubject subject;
6
7 public ConcreteObserver(ConcreteSubject subject, string name)
8 {
9 subject_ = subject;
10 name_ = name;
11 }
12 public override void Update()
13 {
14 observerState_ = subject.SubjectState;
15 }
16
17 public ConcreteSubject Subject
18 {
19 get {return subject;}
20 set {subject = value;}
21 }
22 }
23
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static void Main(string[] args)
2 {
3 ConcreteSubject s = new ConcreteSubject();
4
5 s.Attach(new ConcreteObserver(s, "Jay"));
6 s.Attach(new ConcreteObserver(s, "Lily"));
7
8 s.SubjectState = "hahaha";
9 s.Notify();
10
11 }
12
以上为通用情况下的观察者模型。但是,有部分缺陷,下面将介绍采用了事件委托的观察者模式(摘自《大话设计模式》)。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class StockObserver
2 {
3 private string name;
4 private Subject sub;
5 public StockObserver(string name, Subject sub)
6 {
7 this.name = name;
8 this.sub =sub;
9 }
10
11 //update
12 public void CloseStockMarke()
13 {
14 Console.WriteLine("{0} {1} close stock, go to work!", sub.SubjectState, name);
15 }
16 }
17
18 class NBAObserver
19 {
20 private string name;
21 private Subject sub;
22 public NBAObserver(string name, Subject sub)
23 {
24 this.name = name;
25 this.sub = sub;
26 }
27
28 public void CloseNBALive()
29 {
30 Console.WriteLine("{0} {1}", sub.SubjectState, name);
31 }
32 }
33
1 interface Subject
2 {
3 void Notify();
4 string SubjectState
5 {
6 get;
7 set;
8 }
9 }
10
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Boss : Subject
2 {
3 public event EventHandler Update;
4
5 private string action;
6
7 public void Notify()
8 {
9 Update();
10 }
11
12 public string SubjectState
13 {
14 get { return action; }
15 set { action = value; }
16 }
17 }
18
19 class Secretory : Subject
20 {
21 ...//same as above
22 }
23
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Boss boss = new Boss();
2
3 StockObserver colleague = new StockObserver("Henry", boss);
4 NBAObserver Jordan = new NBAObserver("Kobe", boss);
5
6 boss.Update += new EventHandler(colleague.CloseStockMarket);
7 boss.Update += new EventHandler(Jordan.CloseNBALive);
8
9
10 boss.SubjectState = "I'm coming";
11
12 boss.Notify();
13