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;
publicoverridevoid 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 ABCObserver Y's new state is ABCObserver Z's new state is ABCThis 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;
publicstring 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 { privatestring name; private Stock stock;
// Constructor public Investor(string name) { this.name = name; }
publicvoid 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.10Notified Berkshire of IBM's change to $120.10Notified Sorros of IBM's change to $121.00Notified Berkshire of IBM's change to $121.00Notified Sorros of IBM's change to $120.50Notified Berkshire of IBM's change to $120.50Notified Sorros of IBM's change to $120.75Notified Berkshire of IBM's change to $120.75