C# IObservable與IObserver觀察者模式

C#中提供了IObservable<T>接口和IObserver<T>接口來實現觀察者模式,IObservable<T>至關於Subject(主題)接口,下面咱們就以代嗎來講明下如何利用.net框架提供的觀察者模式接口。框架

 

WeatherData類包含氣溫,溼度,氣壓等屬性。ide

    class WeatherData
    {
        /// <summary>
        /// 氣溫
        /// </summary>
        public string temperature { get; set; }
        /// <summary>
        /// 溼度
        /// </summary>
        public string humility { get; set; }
        /// <summary>
        /// 氣壓
        /// </summary>
        public string pressure { get; set; }
    }

 

WeatherDataPublisher類實現了IObservable接口,實現了Subscribe訂閱方法。this

    class WeatherDataPublisher : IObservable<WeatherData>
    {
        List<IObserver<WeatherData>> observers = new List<IObserver<WeatherData>>();
        /// <summary>
        /// 訂閱主題,將觀察者添加到列表中
        /// </summary>
        /// <param name="observer"></param>
        /// <returns></returns>
        public IDisposable Subscribe(IObserver<WeatherData> observer)
        {
            observers.Add(observer);
            return new Unsubscribe(this.observers, observer);
        }
        /// <summary>
        /// 取消訂閱類
        /// </summary>
        private class Unsubscribe : IDisposable
        {
            List<IObserver<WeatherData>> observers;
            IObserver<WeatherData> observer;
            public Unsubscribe(List<IObserver<WeatherData>> observers
            , IObserver<WeatherData> observer)
            {
                this.observer = observer;
                this.observers = observers;
            }

            public void Dispose()
            {
                if (this.observers != null)
                {
                    this.observers.Remove(observer);
                }
            }
        }
        /// <summary>
        /// 通知已訂閱的觀察者
        /// </summary>
        /// <param name="weatherData"></param>
        private void Notify(WeatherData weatherData)
        {
            foreach (var observer in observers)
            {
                observer.OnNext(weatherData);
            }
        }
        /// <summary>
        /// 接收最新的天氣數據
        /// </summary>
        /// <param name="weatherData"></param>
        public void ReciveNewData(WeatherData weatherData)
        {
            Notify(weatherData);
        }
    }

 

下面咱們創建一個抽象類WeatherDisplayBase實現了IObserver接口,全部的天氣展現板(觀察者)繼承這個抽象類,需實現OnNext方法,即接收到新數據推送後要進行的數據處理展現工做,而且可重寫OnCompleted,OnError方法。spa

    abstract class WeatherDisplayBase : IObserver<WeatherData>
    {
        public virtual void OnCompleted()
        {
        }
        public virtual void OnError(Exception error)
        {
        }
        public abstract void OnNext(WeatherData value);
    }

 

CurrentConditionDisplay類爲當前天氣情況展現板,繼承WeatherDisplayBase抽象類,展現最新的天氣數據。.net

    class CurrentConditionDisplay : WeatherDisplayBase
    {
        public override void OnNext(WeatherData value)
        {
            Console.WriteLine("------------------");
            Console.WriteLine("當前天氣情況板");
            Console.WriteLine(string.Format("溫度:{0}\n溼度:{1}\n氣壓:{2}",
                value.temperature, value.humility, value.pressure));
        }
    }

 

StatisticsConditionDisplay類爲氣溫統計展現板,繼承WeatherDisplayBase抽象類,展現歷史最高溫度,最低溫度,平均溫度。code

    class StatisticsConditionDisplay : WeatherDisplayBase
    {
        List<float> temperatures = new List<float>();
        public override void OnNext(WeatherData value)
        {
            float temperature;
            if (float.TryParse(value.temperature, out temperature))
            {
                temperatures.Add(temperature);
            }
            Console.WriteLine("------------------");
            Console.WriteLine("溫度統計板");
            Console.WriteLine(string.Format("平均溫度:{0}\n最高溫度:{1}\n最低溫度:{2}",
                temperatures.Average(), temperatures.Max(), temperatures.Min()));
        }
    }

 

使用方法orm

class Program
    {
        static void Main(string[] args)
        {
            WeatherDataPublisher publisher = new WeatherDataPublisher();
            CurrentConditionDisplay currentDisplay=new CurrentConditionDisplay();
            StatisticsConditionDisplay statisticsDisplay 
            = new StatisticsConditionDisplay();
            //訂閱當前天氣展現板
            IDisposable currentDisplayUnsubscriber= 
            publisher.Subscribe(currentDisplay);
            //訂閱氣溫統計展現板
            IDisposable statisticsDisplayUnsubscriber = 
            publisher.Subscribe(statisticsDisplay);
             
            for (int i = 0; ; i++)
            {
                WeatherData weatherData = new WeatherData();
               Console.WriteLine("請輸入溫度,溼度,壓力");
               string input = Console.ReadLine();
               var array= input.Split(',');
               weatherData.temperature = array[0];
               weatherData.humility = array[1];
               weatherData.pressure = array[2];
               Console.WriteLine("");
                //將輸入的新的天氣數據傳給天氣數據發佈器
               publisher.ReciveNewData(weatherData);
               Console.WriteLine("=============================");
            }
        }
}
相關文章
相關標籤/搜索