意图intent:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
适用性:
- 当一个抽象模型有两个方面, 其中一个方面依赖于另一方面。将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
- 当对一个对象的改变需要同时改变其它对象, 而不知道具体有多少对象有待改变。
- 当一个对象必须通知其它对象,而它又不能假定其它对象是谁。换言之, 你不希望这些对象是紧密耦合的。
Definition:
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
participants
The classes and/or objects participating in this pattern are:
- Subject (Stock)
- knows its observers. Any number of Observer objects may observe a subject
- provides an interface for attaching and detaching Observer objects.
- ConcreteSubject (IBM)
- stores state of interest to ConcreteObserver
- sends a notification to its observers when its state changes
- Observer (IInvestor)
- defines an updating interface for objects that should be notified of changes in a subject.
- ConcreteObserver (Investor)
- maintains a reference to a ConcreteSubject object
- stores state that should stay consistent with the subject's
- implements the Observer updating interface to keep its state consistent with the subject's
在软件构建过程中,我们需要为某些对象建立一种通知依赖关系,一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知。如果这样的依赖关系过于紧密,将使软件不能很好地抵御变化。使用面向对象技术,可以将这种依赖关系弱化,并形成一种稳定的依赖关系。从而实现软件体系结构的松耦合。
使用面向对象的抽象,observer模式使得我们可以独立地改变目标与观察者,从而使二者之间的依赖关系达到松耦合。目标发送通知时,无需制定观察者,通知(可以携带通知信息作为参数)会自动传播。观察者自己决定是否需要订阅通知,目标对象对此一无所知。在c#中的event中,委托充当了抽象的observer接口,而提供事件的对象充当了目标对象。委托是比抽象observer接口更为松耦合的设计。
Sample code in c#
This structural code demonstrates the Observer pattern in which registered objects are notified of and updated with a state change.
// Observer pattern -- Structural example
|
using System; using System.Collections; namespace DoFactory.GangOfFour.Observer.Structural { // MainApp test application class MainApp { static void Main() { // Configure Observer pattern ConcreteSubject s = new ConcreteSubject(); s.Attach(new ConcreteObserver(s,"X")); s.Attach(new ConcreteObserver(s,"Y")); s.Attach(new ConcreteObserver(s,"Z")); // Change subject and notify observers s.SubjectState = "ABC"; s.Notify(); // Wait for user Console.Read(); } } // "Subject" abstract class Subject { private ArrayList observers = new ArrayList(); 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(); } } } // "ConcreteSubject" class ConcreteSubject : Subject { private string subjectState; // Property public string SubjectState { get{ return subjectState; } set{ subjectState = value; } } } // "Observer" abstract class Observer { public abstract void Update(); } // "ConcreteObserver" class ConcreteObserver : Observer { private string name; private string observerState; private ConcreteSubject subject; // Constructor public ConcreteObserver( ConcreteSubject subject, string name) { this.subject = subject; this.name = name; } public override void Update() { observerState = subject.SubjectState; Console.WriteLine("Observer {0}'s new state is {1}", name, observerState); } // Property public ConcreteSubject Subject { get { return subject; } set { subject = value; } } } }
|
Output
Observer X's new state is ABC
Observer Y's new state is ABC
Observer Z's new state is ABC
This real-world code demonstrates the Observer pattern in which registered investors are notified every time a stock changes value.
// Observer pattern -- Real World example
|
using System; using System.Collections; namespace DoFactory.GangOfFour.Observer.RealWorld { // MainApp test application class MainApp { static void Main() { // Create investors Investor s = new Investor("Sorros"); Investor b = new Investor("Berkshire"); // Create IBM stock and attach investors IBM ibm = new IBM("IBM", 120.00); ibm.Attach(s); ibm.Attach(b); // Change price, which notifies investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; // Wait for user Console.Read(); } } // "Subject" abstract class Stock { protected string symbol; protected double price; private ArrayList investors = new ArrayList(); // Constructor public Stock(string symbol, double price) { this.symbol = symbol; this.price = price; } public void Attach(Investor investor) { investors.Add(investor); } public void Detach(Investor investor) { investors.Remove(investor); } public void Notify() { foreach (Investor investor in investors) { investor.Update(this); } Console.WriteLine(""); } // Properties public double Price { get{ return price; } set { price = value; Notify(); } } public string Symbol { get{ return symbol; } set{ symbol = value; } } } // "ConcreteSubject" class IBM : Stock { // Constructor public IBM(string symbol, double price) : base(symbol, price) { } } // "Observer" interface IInvestor { void Update(Stock stock); } // "ConcreteObserver" class Investor : IInvestor { private string name; private Stock stock; // Constructor public Investor(string name) { this.name = name; } public void Update(Stock stock) { Console.WriteLine("Notified {0} of {1}'s " + "change to {2:C}", name, stock.Symbol, stock.Price); } // Property public Stock Stock { get{ return stock; } set{ stock = value; } } } }
|
Output
Notified Sorros of IBM's change to $120.10
Notified Berkshire of IBM's change to $120.10
Notified Sorros of IBM's change to $121.00
Notified Berkshire of IBM's change to $121.00
Notified Sorros of IBM's change to $120.50
Notified Berkshire of IBM's change to $120.50
Notified Sorros of IBM's change to $120.75
Notified Berkshire of IBM's change to $120.75