《Head First 设计模式》C#版:二、观察者模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenfujun818/article/details/79954970
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Observer pattern 观察者模式
/// 模式定义 :定义对象之间一对多依赖,这样一来,当一个对象状态改变时,它的所有依赖都会收到通知并自动更新
/// 设计原则 : 为了交互对象之间的松耦合设计而努力
/// </summary>
public class ObserverPattern : MonoBehaviour {
    void Start () {
        //实例化一个天气数据
        WeatherData data = new WeatherData();
        //实例化一个通知面板
        CurrentConditionDisplay conditionDisplay = new CurrentConditionDisplay(data);
        //通知观察者天气数据变化了
        data.SetMeasurements(123, 456, 789);
        data.SetMeasurements(1233, 4566, 7899);
        //移除观察者
        conditionDisplay.RemoveObserver();
       //这次天气变化就收不到了 
        data.SetMeasurements(12133, 45166, 78919);




    }

}
// ==========  气象站  =========

/// <summary>
/// Current condition display. 布告板类  继承 Observer 是为了从weatherData里面获取 通知
/// DisplayElement 是布告板必须继承的接口
/// </summary>
public class CurrentConditionDisplay : DisplayElement,Observer{

    private float temp;
    private float humi;
    /// <summary>
    /// 此处用subject 是为了方便操作 remove 接口
    /// </summary>
    public Subject weather;
    public CurrentConditionDisplay(Subject data){
        this.weather = data;
        this.weather.RegisterObserver(this);
    }

    public void RemoveObserver()
    {
        this.weather.RomoveObserver(this);
    }
    public void Update(float temp, float humidity, float pressure){
        this.temp = temp;
        this.humi = humidity;
        Display();
    }

    public void Display(){
        Debug.Log("temp = " + temp + " humi = " + humi);
    }
}

/// <summary>
/// Weather data.
/// </summary>
public class WeatherData : Subject{
    private List<Observer> observers;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData(){
        observers = new List<Observer>();
    }

    public void RegisterObserver(Observer observer){
        observers.Add(observer);
    }

    public void RomoveObserver(Observer observer){
       int index = observers.IndexOf(observer);
        if(index >= 0)
            observers.RemoveAt(index);
    }
    /// <summary>
    /// 通知每一个观察者  因为接口实现了update
    /// </summary>
    /// <param name="observer">Observer.</param>
    public void NotifyObserver(){
        int count = observers.Count;
        for (int index = 0; index < count;index ++){
            observers[index].Update(temperature,humidity,pressure);
        }
    }
    /// <summary>
    /// Measurementses the changed. 当气象站变化的时候 通知每一个观察者
    /// </summary>
    public void MeasurementsChanged(){
        NotifyObserver();
    }

    /// <summary>
    /// 天气变化从这个方法告诉 天气Data类  
    /// </summary>
    /// <param name="temp">Temp.</param>
    /// <param name="hunm">Hunm.</param>
    /// <param name="pressure">Pressure.</param>
    public void SetMeasurements(float temp,float hunm,float pressure){
        this.temperature = temp;
        this.pressure = pressure;
        this.humidity = hunm;
        MeasurementsChanged();
    }



}

/// <summary>
/// 观察主题 
/// </summary>
public interface Subject {
    /// <summary>
    /// 注册观察者
    /// </summary>
    /// <param name="observer">Observer.</param>
    void RegisterObserver(Observer observer);
    /// <summary>
    /// 移除观察者
    /// </summary>
    /// <param name="observer">Observer.</param>
    void RomoveObserver(Observer observer);
    /// <summary>
    /// 当主题改变时 通过接口 通知所有观察者 
    /// </summary>
    void NotifyObserver();

}

/// <summary>
/// Observer. 观察者 
/// </summary>
public interface Observer{
    void Update(float temp, float humidity, float pressure);
}

/// <summary>
/// 显示通知内容
/// </summary>
public interface DisplayElement{
    void Display(); 
}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页