public StockObserver(StockGrabber SG) { var stockGrabber = new StockGrabber(); this.observerID = ++observerIDTracker; Console.WriteLine("New observer " + this.observerID); stockGrabber.register(this); }
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(); }
static void Main(string[] args) { StockGrabber stockGrabber = new StockGrabber(); StockObserver observer1 = new StockObserver(stockGrabber); stockGrabber.setStockPrice(123.12); }
static void Main(string[] args) { StockGrabber stockGrabber = new StockGrabber(); StockObserver observer01 = new StockObserver(stockGrabber); stockGrabber.setIBMPrice(190.50); stockGrabber.setApplePrice(670.25); stockGrabber.setGooglePrice(700.75); Console.ReadLine(); }
/* * 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(); }
static void Main(string[] args) { StockGrabber stockGrabber = new StockGrabber(); StockObserver observer1 = new StockObserver(stockGrabber); stockGrabber.setIBMPrice(197.00); stockGrabber.setAAPLPrice(247.00); stockGrabber.setGOOGLPrice(537.00); Console.Read(); }
static void Main(string[] args) { StockGrabber stockGrabber = new StockGrabber(); StockObserver observerOne = new StockObserver(stockGrabber); stockGrabber.setIBMPrice(157.25); stockGrabber.setAAPLPrice(257.54); stockGrabber.setGOOPrice(358.84); StockObserver observerTwo = new StockObserver(stockGrabber); stockGrabber.setIBMPrice(158.65); stockGrabber.setAAPLPrice(247.67); stockGrabber.setGOOPrice(375.25); }