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