static void Main(string[] args) { StockGrabber stockGrabber = new StockGrabber(); StockObserver observer1 = new StockObserver(); stockGrabber.Register(observer1); stockGrabber.IbmPrice = 197.00; stockGrabber.AaplPrice = 197.00; stockGrabber.GoogPrice = 197.00; stockGrabber.NotifyObservers(); StockObserver observer2 = new StockObserver(); stockGrabber.Register(observer2); stockGrabber.IbmPrice = 297.00; stockGrabber.AaplPrice = 297.00; stockGrabber.GoogPrice = 297.00; stockGrabber.Unregister(observer1); stockGrabber.NotifyObservers(); var getIBM = new GetTheStock(stockGrabber, 2, "IBM", 197.00); var getAAPL = new GetTheStock(stockGrabber, 2, "AAPL", 677.60); var getGOOG = new GetTheStock(stockGrabber, 2, "GOOG", 676.40); new Thread(new ThreadStart(getIBM.Run)).Start(); new Thread(new ThreadStart(getAAPL.Run)).Start(); new Thread(new ThreadStart(getGOOG.Run)).Start(); }
/* * Defines one-to-many dependencies between objects so that the state one changes, all dependent objects are notified. * Contains two main actors, Subject which notifies change and Observer that receives and acts on updates * Loose coupling is a benefit. However, Subject may send updates that don't matter to Observer. */ static void Main(string[] args) { var stockGrabber = new StockGrabber(); var observer1 = new Observer1(); var observer2 = new Observer2(); stockGrabber.Register(observer1); stockGrabber.Register(observer2); stockGrabber.SetPrice(100.00); Console.ReadLine(); }