17. Observer mode (Observer pattern)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.