定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
名称 |
Observer |
结构 |
![](/images/cnblogs_com/darkangel/Observer.gif) |
意图 |
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。 |
适用性 |
- 当一个抽象模型有两个方面, 其中一个方面依赖于另一方面。将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
- 当对一个对象的改变需要同时改变其它对象, 而不知道具体有多少对象有待改变。
- 当一个对象必须通知其它对象,而它又不能假定其它对象是谁。换言之, 你不希望这些对象是紧密耦合的。
|
Code Example |
1 // Observer 2![](/Images/OutliningIndicators/None.gif) 3 // Intent: "Define a one-to-many dependency between objects so that when one 4 // object changes state, all its dependents are notified and 5 // updated automatically". 6![](/Images/OutliningIndicators/None.gif) 7 // For further information, read "Design Patterns", p293, Gamma et al., 8 // Addison-Wesley, ISBN:0-201-63361-2 9![](/Images/OutliningIndicators/None.gif) 10![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* Notes: 11 * Often used for Document-View architectures, where a document stores 12 * data and one or more views renders the data. 13 */ 14 15 namespace Observer_DesignPattern 16![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) { 17 using System; 18 using System.Collections; 19![](/Images/OutliningIndicators/InBlock.gif) 20 class Subject 21![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 22 private ArrayList list = new ArrayList(); 23![](/Images/OutliningIndicators/InBlock.gif) 24 private string strImportantSubjectData = "Initial"; 25 26 public string ImportantSubjectData 27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 28 get 29![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 30 return strImportantSubjectData; 31 } 32 set 33![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 34 strImportantSubjectData = value; 35 } 36 } 37![](/Images/OutliningIndicators/InBlock.gif) 38 public void Attach(Observer o) 39![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 40 list.Add(o); 41 o.ObservedSubject = this; 42 } 43![](/Images/OutliningIndicators/InBlock.gif) 44 public void Detach(Observer o) 45![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 46 47 } 48![](/Images/OutliningIndicators/InBlock.gif) 49 public void Notify() 50![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 51 foreach (Observer o in list) 52![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 53 o.Update(); 54 } 55 } 56 } 57![](/Images/OutliningIndicators/InBlock.gif) 58 class ConcreteSubject : Subject 59![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 60 public void GetState() 61![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 62 63 } 64![](/Images/OutliningIndicators/InBlock.gif) 65 public void SetState() 66![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 67 68 } 69 } 70![](/Images/OutliningIndicators/InBlock.gif) 71 abstract class Observer 72![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 73 protected Subject s; 74 public Subject ObservedSubject 75![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 76 get 77![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 78 return s; 79 } 80 set 81![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 82 s = value; 83 } 84 } 85 abstract public void Update(); 86 } 87![](/Images/OutliningIndicators/InBlock.gif) 88 class ConcreteObserver : Observer 89![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 90 private string observerName; 91 92 public ConcreteObserver(string name) 93![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 94 observerName = name; 95 } 96![](/Images/OutliningIndicators/InBlock.gif) 97 override public void Update() 98![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 99 Console.WriteLine("In Observer {0}: data from subject = {1}", 100 observerName, s.ImportantSubjectData); 101 } 102 } 103![](/Images/OutliningIndicators/InBlock.gif) 104![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 105 /// Summary description for Client. 106 /// </summary> 107 public class Client 108![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 109 public static int Main(string[] args) 110![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 111 // Set up everything 112 ConcreteSubject s = new ConcreteSubject(); 113 ConcreteObserver o1 = new ConcreteObserver("first observer"); 114 ConcreteObserver o2 = new ConcreteObserver("second observer"); 115![](/Images/OutliningIndicators/InBlock.gif) 116 s.Attach(o1); 117 s.Attach(o2); 118![](/Images/OutliningIndicators/InBlock.gif) 119 // make changes to subject 120 s. ImportantSubjectData = "This is important subject data"; 121![](/Images/OutliningIndicators/InBlock.gif) 122 // Notify all observers 123 s.Notify(); 124 return 0; 125 } 126 } 127 } 128![](/Images/OutliningIndicators/None.gif) |