- using System;
- using System.Collections;
- namespace WeatherStation
- {
- public class CRunMain
- {
- public static int Main()
- {
- CWeatherData objWeather = null;
- CCurentConditionsDisplay objCurrent = null;
- CFurtureConditionsDisplay objFurture = null;
- objWeather = new CWeatherData();
- objCurrent = new CCurentConditionsDisplay(objWeather);
- objFurture = new CFurtureConditionsDisplay(objWeather);
- Console.WriteLine("first weather:");
- objWeather.SetMeasurements(10.0f, 65.0f, 20.0f);
- Console.WriteLine("/nweather changed in first time:");
- objWeather.SetMeasurements(11.0f, 66.0f, 21.0f);
- Console.WriteLine("/nweather changed in second time:");
- objWeather.SetMeasurements(12.0f, 67.0f, 22.0f);
- return 0;
- }
- };
- public interface ISubject
- {
- void RegisterObserver(IObserver objObser);
- void RemoveObserver(IObserver objObser);
- void NotifyObservers();
- };
- public interface IObserver
- {
- void Update(float fTmplat, float fHmdy, float fPress);
- };
- public interface IDisplayElement
- {
- void Display();
- };
- public class CWeatherData:ISubject
- {
- public CWeatherData()
- {
- this.m_lsObj = new ArrayList();
- }
- //ISubject Members
- public void RegisterObserver(IObserver objObser)
- {
- this.m_lsObj.Add(objObser);
- }
- public void RemoveObserver(IObserver objObser)
- {
- int i = 0;
- i = this.m_lsObj.IndexOf(objObser);
- if (i >= 0)
- {
- this.m_lsObj.RemoveAt(i);
- }
- }
- public void NotifyObservers()
- {
- int i = 0;
- IObserver objObser = null;
- for (i = 0; i < this.m_lsObj.Count; i++)
- {
- objObser = (IObserver)this.m_lsObj[i];
- objObser.Update(this.m_fTmplat, this.m_fHmdy, this.m_fPress);
- }
- }
- //owner function
- public void MesssurementsChanged()
- {
- this.NotifyObservers();
- }
- public void SetMeasurements(float fTmplat, float fHmdy, float fPress)
- {
- this.m_fTmplat = fTmplat;
- this.m_fHmdy = fHmdy;
- this.m_fPress = fPress;
- this.MesssurementsChanged();
- }
- private ArrayList m_lsObj;
- private float m_fTmplat;
- private float m_fHmdy;
- private float m_fPress;
- };
- public class CCurentConditionsDisplay:IObserver, IDisplayElement
- {
- public CCurentConditionsDisplay(ISubject objSub)
- {
- this.m_fHumdy = 0.0f;
- this.m_fPress = 0.0f;
- this.m_fTmplat = 0.0f;
- this.m_objSub = objSub;
- if (null != this.m_objSub)
- {
- this.m_objSub.RegisterObserver(this);
- }
- }
- //IObserver Members
- public void Update(float fTmplat, float fHmdy, float fPress)
- {
- this.m_fTmplat = fTmplat;
- this.m_fPress = fPress;
- this.m_fHumdy = fHmdy;
- this.Display();
- }
- //IDisplayElement Members
- public void Display()
- {
- Console.WriteLine("Current Conditions:/nTemperature: " + this.m_fTmplat + " Degress/tHumidity = " + this.m_fHumdy
- + "% Humidity/tPress = " + this.m_fPress);
- }
- private float m_fTmplat;
- private float m_fHumdy;
- private float m_fPress;
- private ISubject m_objSub;
- }
- public class CFurtureConditionsDisplay:IObserver, IDisplayElement
- {
- public CFurtureConditionsDisplay(ISubject objSub)
- {
- this.m_fHumdy = 0.0f;
- this.m_fPress = 0.0f;
- this.m_fTmplat = 0.0f;
- this.m_objSub = objSub;
- if (null != this.m_objSub)
- {
- this.m_objSub.RegisterObserver(this);
- }
- }
- //IObserver Members
- public void Update(float fTmplat, float fHmdy, float fPress)
- {
- this.m_fTmplat = fTmplat;
- this.m_fPress = fPress;
- this.m_fHumdy = fHmdy;
- this.Display();
- }
- //IDisplayElement Members
- public void Display()
- {
- Console.WriteLine("/nFurture Conditions:/nTemperature: " + this.m_fTmplat + " Degress/tHumidity = " + this.m_fHumdy
- + "% Humidity/tPress = " + this.m_fPress);
- }
- private float m_fTmplat;
- private float m_fHumdy;
- private float m_fPress;
- private ISubject m_objSub;
- }
- };