usingSystem;usingSystem.Collections.Generic;namespaceconsoleapplication10{/// <summary> ///In the process of software building, we need to establish a "notification dependency" for some objects///--The state of an object (the target object) is changed, and all dependent objects (the Observer object) are notified. ///if such dependencies are too tight, the software will not be able to withstand changes very well. Using object-oriented technology, you can weaken this dependency,///and to form a stable dependency relationship. So as to realize the loose coupling of software architecture. /// </summary> classProgram {Static voidMain (string[] args) {Stock ms=NewMicrosoft ("Microsoft",120.00); Ms. Addobserver (NewInvestor ("Jom")); Ms. Addobserver (NewInvestor ("Terrylee")); Ms. Update (); Console.ReadLine (); } } /// <summary> ///Stock/// </summary> Public Abstract classStock {PrivateList<iobserver> observers =NewList<iobserver>(); PrivateString _symbol; Private Double_price; PublicStock (String symbol,DoublePrice ) { This. _symbol =symbol; This. _price =Price ; } /// <summary> ///Send Message/// </summary> Public voidUpdate () {foreach(IObserver obinchobservers) {ob. SendData ( This); } } Public voidAddobserver (IObserver observer) {observers. ADD (Observer); } Public voidRemoveobserver (IObserver observer) {observers. REMOVE (Observer); } PublicString Symbol {Get{return_symbol;} } Public DoublePrice {Get{return_price;} } } /// <summary> ///Microsoft's stock/// </summary> Public classMicrosoft:stock { PublicMicrosoft (String symbol,DoublePrice ):Base(symbol, price) {}} Public InterfaceIObserver {voidSendData (stock stock); } Public classInvestor:iobserver {Private string_name; PublicInvestor (stringname) { This. _name =name; } Public voidSendData (stock stock) {Console.WriteLine ("notified {0} of {1} ' s"+"Change to {2:C}", _name, stock. Symbol, stock. Price); } }}
usingSystem;usingSystem.Collections.Generic;namespaceconsoleapplication10{/// <summary> ///In the process of software building, we need to establish a "notification dependency" for some objects///--The state of an object (the target object) is changed, and all dependent objects (the Observer object) are notified. ///if such dependencies are too tight, the software will not be able to withstand changes very well. Using object-oriented technology, you can weaken this dependency,///and to form a stable dependency relationship. So as to realize the loose coupling of software architecture. /// </summary> classProgram {Static voidMain (string[] args) {Stock Stock=NewStock ("Microsoft",120.00); Investor Investor=NewInvestor ("Jom"); Investor Investor1=NewInvestor ("Sam"); Stock. Notifyevent+=NewNotifyeventhandler (investor. SendData); Stock. Notifyevent+=NewNotifyeventhandler (Investor1. SendData); Stock. Update (); Console.ReadLine (); } } Public Delegate voidNotifyeventhandler (Objectsender); Public classStock { PublicNotifyeventhandler notifyevent; PrivateString _symbol; Private Double_price; PublicStock (String symbol,DoublePrice ) { This. _symbol =symbol; This. _price =Price ; } Public voidUpdate () {onnotifychange (); } Public voidOnnotifychange () {if(Notifyevent! =NULL) {notifyevent ( This); } } PublicString Symbol {Get{return_symbol;} } Public DoublePrice {Get{return_price;} } } Public classInvestor {Private string_name; PublicInvestor (stringname) { This. _name =name; } Public voidSendData (Objectobj) { if(obj isStock) {Stock Stock=(Stock) obj; Console.WriteLine ("notified {0} of {1} ' s"+"Change to {2:C}", _name, stock. Symbol, stock. Price); } } }}
17. Observer mode (Observer pattern)