Observer 观察者模式 (HeadFirst设计模式 c#)

Observer.cs

 

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

namespace Observer
{
   

    public interface ISubject
    {
        void RegisterObserver(IOvserver o);
        void RemoveObserver(IOvserver o);
        void NotifyObserver();
    }

    public interface IOvserver
    {
        void update(float temp, float humidity, float pressure);
    }

    public interface IDisplayElement
    {
        void display();
    }

    public class WeatherData:ISubject
    {
        private List<IOvserver> obList;
        private float temperature;
        private float humidity;
        private float pressure;

        public WeatherData()
        {
            obList = new List<IOvserver>();
        }

        public void RegisterObserver(IOvserver o)
        {
            obList.Add(o);
        }

        public void RemoveObserver(IOvserver o)
        {

            if (obList.IndexOf(o) >= 0)
            {
                obList.Remove(o);
            }
        }

        public void NotifyObserver()
        {
            foreach (IOvserver ob in obList)
            {
                ob.update(temperature, humidity, pressure);
            }
        }

        public void measureChanged()
        {
            NotifyObserver();
        }

        public void setMesureData(float temperature, float humidity, float pressure)
        {
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            measureChanged();
        }
    }

    public class CurrentConditionDisplay : IOvserver, IDisplayElement
    {
        float temperature, humidity, pressure;
        ISubject weatherData;

        public CurrentConditionDisplay(ISubject weatherData)
        {
            this.weatherData = weatherData;
            weatherData.RegisterObserver(this);
        }

        public void update(float temperature, float humidity, float pressure)
        {
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            display();
        }

        public void display()
        {
            float pp = computeHeatIndex(temperature, humidity);
            Console.WriteLine(@"Current Condition:" + temperature + "F degrees and" + humidity + "%humidity"
                                + " RH: " + pp);
        }

        private float computeHeatIndex(float t, float rh)
        {
            float index = (float)((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) +
                (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) +
                (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +
                (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 *
                (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +
                (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +
                0.000000000843296 * (t * t * rh * rh * rh)) -
                (0.0000000000481975 * (t * t * t * rh * rh * rh)));
            return index;
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值