/*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