» The Observer pattern defines a relationship between objects so that when one changes its state, all the others are notified accordingly; there is usually an identifiable single publisher of new state, and many subscribers who wish to receive it; » The Observer pattern supports both push and pull models; » The Observer pattern is widely used in blog sites and various software systems, like Flickr, cyber-dating web sites, and Microsoft Updates are a few of the places where it is applied; » The Observer pattern also has a formal face as the "publish/subscribe" model and the "Model-View-Controller" (MVC) architectural pattern.
Use When:
» There are aspects to an abstraction that can vary independently; » Changes in one object need to be propagated to a selection of other objects, not all of them; » The object sending the changes does not need to know about the receivers.
//The Stock class provides an interface for attaching and detaching Investor objects, it knows its Investors. //Any number of Investor objects may observe. public classStock { private string _symbol; private double _price; private List _investors = newList<IInvestor>();
public void Attach(IInvestor investor) { _investors.Add(investor); }
public void Detach(IInvestor investor) { _investors.Remove(investor); }
public void Notify() { foreach (IInvestor investor in _investors) { investor.Update(this); } }
public double Price { get { return _price; } set { if (_price != value) { _price = value; Notify(); } } }
public string Symbol { get { return _symbol; } } }
//The IBM class stores state of interest to Investor and sends a notification to its observers when its state changes. public classIBM : Stock { public IBM(string symbol, double price) : base(symbol, price) { } }
//The IInvestor interface defines an updating interface for objects that should be notified of changes in an Stock's status. public interfaceIInvestor { void Update(Stock stock); }
//The Investor class maintains a reference to a Stock object, stores state that should stay consistent with the Stock's state //and implements the IInvestor updating interface to keep its state consistent with the Stock's state. public classInvestor : IInvestor { private string _name; privateStock _stock;
public Investor(string name) { _name = name; }
public void Update(Stock stock) { Console.WriteLine("Notified {0} of {1}'s change to {2:C}", _name, stock.Symbol, stock.Price); }
publicStock Stock { get { return _stock; } set { _stock = value; } } }