观察者模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JackYong.Observer
{
    /// <summary>
    /// 观察者
    /// </summary>
    public interface ObServer
    {
        /// <summary>
        /// 当气象观测值改变时,主题会把这些状态值当作方法的参数,传送给观察者,所有的观察者都必须实现update()方法,以实现观测者接口,在这里我们按照mary 和sue的想法把观测值传入观察者中。
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="humidity"></param>
        /// <param name="pressure"></param>
         void update(float temp, float humidity, float pressure);
    }

}

DisplayElement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JackYong.Observer
{

    /// <summary>
    /// dispaly Element 接口值包含一了一个方法,也就是display (),当公告板需要显示时,调用此方法。
    /// </summary>
    public interface DisplayElement
    {
        void Display();
    }

}

 主题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JackYong.Observer
{
    /// <summary>
    /// 主题  
    /// </summary>
    public interface Subject
    {
        /// <summary>
        /// 这两个方法都需要一个观察者作为变量,该观察者是用来注册或被删除的。
        /// </summary>
        /// <param name="o"></param>
        void registerObserver(ObServer o);

        /// <summary>
        /// 移除观察者
        /// </summary>
        /// <param name="o"></param>
        void removeObserver(ObServer o);

        /// <summary>
        /// 当主题状态改变时,这个方法会被调用,以通知所有的观察者
        /// </summary>
        void notifyObservers();


    }




}

 主题的实现类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace JackYong.Observer
{

    /// <summary>
    /// weatherData 实现了subject接口
    /// </summary>
    public class WeatherData : Subject
    {
        /// <summary>
        /// 加上ArrayList来记录观察者,此ArrayList是在构造器中建立的。
        /// </summary>
        private ArrayList observers;
        private float temperature;
        private float humidity;
        private float pressure;

        public WeatherData()
        {

            observers = new ArrayList();
        }

        /// <summary>
        /// 注册 
        /// </summary>
        /// <param name="o"></param>
        public void registerObserver(ObServer o)
        {
            observers.Add(o);
        }


        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="o"></param>
        public void removeObserver(ObServer o)
        {
            int i = observers.IndexOf(o);
            if (i >= 0)
            {
                observers.Remove(i);
            }
        }

        /// <summary>
        /// 通知
        /// </summary>
        public void notifyObservers()
        {
            for (int i = 0; i < observers.Count; i++)
            {
                ObServer observer = (ObServer)observers[i];
                observer.update(temperature, humidity, pressure);
            }
        }

        public void measurementsChanged()
        {
            notifyObservers();
        }

        /// <summary>
        /// 当气候改变后调用
        /// </summary>
        /// <param name="temperature"></param>
        /// <param name="humidity"></param>
        /// <param name="pressure"></param>
        public void setMeasurements(float temperature, float humidity, float pressure)
        {
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            measurementsChanged();
        }


    }
}

统计量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JackYong.Observer
{
    /// <summary>
    /// 统计
    /// </summary>
    public class StatisticsDisplay : ObServer, DisplayElement
    {
        private float temperature;
        private float humidity;
        private Subject weatherData;

        public StatisticsDisplay(Subject weatherData)
        {
            this.weatherData = weatherData;
            weatherData.registerObserver(this);
        }

        public void update(float temp, float humidity, float pressure)
        {
            this.temperature = temp;
            this.humidity = humidity;
            //更新完毕后 显示
            Display();
        }



        public void Display()
        {
            Console.WriteLine("current coditions :" + temperature + " F degrees and " + humidity + "% humidity");
        }

    }
}

 当前

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JackYong.Observer
{
   public class CurrentConditionsDisplay:ObServer,DisplayElement
    {
       private float temperature;
       private float humidity;
       private Subject weatherData;

       public CurrentConditionsDisplay(Subject weatherData)
       {
           this.weatherData = weatherData;
           weatherData.registerObserver(this);
       }

       public void update(float temp, float humidity, float pressure)
       {
           this.temperature = temp;
           this.humidity = humidity;
           //更新完毕后 显示
           Display();
       }

      

        public void Display()
        {
            Console.WriteLine("current coditions :" + temperature + " F degrees and " + humidity + "% humidity");
        }

      
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JackYong.Observer
{
    class Program
    {
        static void Main(string[] args)
        {
          
            WeatherData weatherdata = new WeatherData();
            CurrentConditionsDisplay currentdisplay = new CurrentConditionsDisplay(weatherdata);
            StatisticsDisplay statistics = new StatisticsDisplay(weatherdata);
            weatherdata.setMeasurements(80, 65, 30.4f);
            weatherdata.setMeasurements(82, 70, 29.2f);
            weatherdata.setMeasurements(78, 90, 29.2f);

        }
    }
}
posted @ 2011-06-30 23:49  jackyong  阅读(1135)  评论(0编辑  收藏  举报