observer pattern(观察者模式)应用之气象站(C#源代码)

 
  1. using System;
  2. using System.Collections;
  3. namespace WeatherStation
  4. {
  5.     public class CRunMain
  6.     {
  7.         public static int Main()
  8.         {
  9.             CWeatherData objWeather = null;
  10.             CCurentConditionsDisplay objCurrent = null;
  11.             CFurtureConditionsDisplay objFurture = null;
  12.             objWeather = new CWeatherData();
  13.             objCurrent = new CCurentConditionsDisplay(objWeather);
  14.             objFurture = new CFurtureConditionsDisplay(objWeather);
  15.             Console.WriteLine("first weather:");
  16.             objWeather.SetMeasurements(10.0f, 65.0f, 20.0f);
  17.             Console.WriteLine("/nweather changed in first time:");
  18.             objWeather.SetMeasurements(11.0f, 66.0f, 21.0f);
  19.             Console.WriteLine("/nweather changed in second time:");
  20.             objWeather.SetMeasurements(12.0f, 67.0f, 22.0f);
  21.             return 0;
  22.         }
  23.     };
  24.     public interface ISubject
  25.     {
  26.         void RegisterObserver(IObserver objObser);
  27.         void RemoveObserver(IObserver objObser);
  28.         void NotifyObservers();
  29.     };
  30.     public interface IObserver
  31.     {
  32.         void Update(float fTmplat, float fHmdy, float fPress);
  33.     };
  34.     public interface IDisplayElement
  35.     {
  36.         void Display();
  37.     };
  38.     public class CWeatherData:ISubject
  39.     {
  40.         public CWeatherData()
  41.         {
  42.             this.m_lsObj = new ArrayList();
  43.         }
  44.         //ISubject Members
  45.         public void RegisterObserver(IObserver objObser)
  46.         {
  47.             this.m_lsObj.Add(objObser);
  48.         }
  49.         public void RemoveObserver(IObserver objObser)
  50.         {
  51.             int i = 0;
  52.             
  53.             i = this.m_lsObj.IndexOf(objObser);
  54.             if (i >= 0)
  55.             {
  56.                 this.m_lsObj.RemoveAt(i);
  57.             }
  58.         }
  59.         public void NotifyObservers()
  60.         {
  61.             int i = 0;
  62.             IObserver objObser = null;
  63.             for (i = 0; i < this.m_lsObj.Count; i++)
  64.             {
  65.                 objObser = (IObserver)this.m_lsObj[i];
  66.                 objObser.Update(this.m_fTmplat, this.m_fHmdy, this.m_fPress);
  67.             }
  68.         }   
  69.         //owner function
  70.         public void MesssurementsChanged()
  71.         {
  72.             this.NotifyObservers();
  73.         }
  74.         public void SetMeasurements(float fTmplat, float fHmdy, float fPress)
  75.         {
  76.             this.m_fTmplat = fTmplat;
  77.             this.m_fHmdy = fHmdy;
  78.             this.m_fPress = fPress;
  79.             this.MesssurementsChanged();
  80.         }       
  81.         private ArrayList m_lsObj;
  82.         private float m_fTmplat;
  83.         private float m_fHmdy;
  84.         private float m_fPress; 
  85.         
  86.     };
  87.     public class CCurentConditionsDisplay:IObserver, IDisplayElement
  88.     {
  89.         public CCurentConditionsDisplay(ISubject objSub)
  90.         {
  91.             this.m_fHumdy = 0.0f;
  92.             this.m_fPress = 0.0f;
  93.             this.m_fTmplat = 0.0f;
  94.             this.m_objSub = objSub;
  95.             if (null != this.m_objSub)
  96.             {
  97.                 this.m_objSub.RegisterObserver(this);
  98.             }
  99.         }
  100.         //IObserver Members
  101.         public void Update(float fTmplat, float fHmdy, float fPress)
  102.         {
  103.             this.m_fTmplat = fTmplat;
  104.             this.m_fPress = fPress;
  105.             this.m_fHumdy = fHmdy;
  106.             this.Display();
  107.         }
  108.         //IDisplayElement Members
  109.         public void Display()
  110.         {
  111.             Console.WriteLine("Current Conditions:/nTemperature: " + this.m_fTmplat + " Degress/tHumidity = " + this.m_fHumdy
  112.                 + "% Humidity/tPress = " + this.m_fPress);
  113.         }
  114.         private float m_fTmplat;
  115.         private float m_fHumdy;
  116.         private float m_fPress;
  117.         private ISubject m_objSub;
  118.     }
  119.     public class CFurtureConditionsDisplay:IObserver, IDisplayElement
  120.     {
  121.         public CFurtureConditionsDisplay(ISubject objSub)
  122.         {
  123.             this.m_fHumdy = 0.0f;
  124.             this.m_fPress = 0.0f;
  125.             this.m_fTmplat = 0.0f;
  126.             this.m_objSub = objSub;
  127.             if (null != this.m_objSub)
  128.             {
  129.                 this.m_objSub.RegisterObserver(this);
  130.             }
  131.         }
  132.         //IObserver Members
  133.         public void Update(float fTmplat, float fHmdy, float fPress)
  134.         {
  135.             this.m_fTmplat = fTmplat;
  136.             this.m_fPress = fPress;
  137.             this.m_fHumdy = fHmdy;
  138.             this.Display();
  139.         }
  140.         //IDisplayElement Members
  141.         public void Display()
  142.         {
  143.             Console.WriteLine("/nFurture Conditions:/nTemperature: " + this.m_fTmplat + " Degress/tHumidity = " + this.m_fHumdy
  144.                 + "% Humidity/tPress = " + this.m_fPress);
  145.         }
  146.         private float m_fTmplat;
  147.         private float m_fHumdy;
  148.         private float m_fPress;
  149.         private ISubject m_objSub;
  150.     }
  151. };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值