Weather Station--observer mode

Source: Internet
Author: User

    /*Weather Stations*/     Public InterfaceSubject {voidregist (Observer Observer); voidRemove (Observer Observer); voidNotify (); }     Public InterfaceObserver {voidUpDate (Weatherdata weahterdata); }     Public Interfacedisplayelement {voidDisplay (); }     Public classWeatherdata:subject {Private floatm_temperature;  Public floatTemperature {Get{returnM_temperature;} Set            {                if(M_temperature! =value) {M_temperature=value;                Notify (); }            }        }        PrivateList<observer> m_listobservers =NewList<observer>();  Public voidregist (Observer Observer) {if(!M_listobservers.contains (Observer))            {M_LISTOBSERVERS.ADD (Observer); }        }         Public voidRemove (Observer Observer) {if(M_listobservers.contains (Observer)) {M_LISTOBSERVERS.REMOVE (Observer); }        }         Public voidNotify () { for(inti =0; i < M_listobservers.count; i++) {M_listobservers[i]. UpDate ( This); }        }    }     Public classdisplayweathera:displayelement, Observer {floattemperature;  Public voidDisplay () {Console.WriteLine (temperature); }         Public voidUpDate (Weatherdata weahterdata) {temperature=weahterdata.temperature; }    }     Public classdisplayweatherb:displayelement, Observer {floattemperature;  Public voidDisplay () {Console.WriteLine (temperature); }         Public voidUpDate (Weatherdata weahterdata) {temperature=weahterdata.temperature; }    }    classProgram {Static voidMain (string[] args) {Displayweathera DWA=NewDisplayweathera (); Displayweatherb DWB=NewDisplayweatherb (); Weatherdata WD=NewWeatherdata (); Wd.            Regist (DWA); Wd.            Regist (DWB); Wd. Temperature= +; Dwa.            Display (); Dwb.            Display ();        Console.readkey (); }    }

Weather Station--observer mode

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.