Head First 设计模式----ObserverPattern

观察者模式在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖它的对象都会收到通知,并自动更新。

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

namespace HeadFirst.Observer
{
    public interface Subject
    {
         void registerObserver(Observer o);
         void removeObserver(Observer o);
         void notifyObservers();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HeadFirst.Observer
{
   public interface Observer
    {
         void update(float temp, float humidity, float pressure);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HeadFirst.Observer
{
   public interface DisplayElement
    {
         void display();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace HeadFirst.Observer
{ 

    class WeatherData:Subject
    {
        private ArrayList observers;
        private float temperature;
        private float humidity;
        private float pressure;

        public WeatherData() { 
            observers=new ArrayList();
        }

        public void registerObserver(Observer o) {
            observers.Add(o);
        }

        public void removeObserver(Observer o) {
            int i = observers.IndexOf(o);
            if(i>=0){
                observers.Remove(i);
            }        
        }

        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();
        }

        public void setMeasurements(float temperature,float humidity,float pressure) {
            this.temperature = temperature;
            this.humidity = humidity;
            this.pressure = pressure;
            measurementsChanged();        
        }
        //WeatherData的其它方法
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HeadFirst.Observer
{    //目前状况布告板
    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 temperature, float humidity, float pressure) {
            this.temperature = temperature;
            this.humidity = humidity;
            display();
        }
        public void display() {
            Console.Write("Current conditions:"+temperature+" F degrees and "+humidity+" % humidity");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeadFirst.Decorator;
using HeadFirst.Command;
using HeadFirst.Observer;


namespace HeadFirst
{
    class Program
    {
        static void Main(string[] args)
        {
          
            //观察者模式
            WeatherData weatherdata = new WeatherData();
            CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherdata);
            weatherdata.setMeasurements(80, 65, 30.4f);
            Console.ReadLine();
            
        }
    }
}
原文地址:https://www.cnblogs.com/SmileX/p/4202112.html