说明:主要参考《Head First设计模式(中文版)》,使用C#代码实现。
代码:Github
1、观察者模式UML图
2、气象监测类图
3、气象监测代码(书中C#版)
3.1 Observer
public interface IObserver { void Update(float temperature, float humidity, float pressure); }
public interface IDisplayElement { void Display(); }
public class CurrentConditionDisplay : IObserver, IDisplayElement { private readonly ISubject _weatherData; private float _temperature; private float _humidity; public CurrentConditionDisplay(ISubject weatherData) { _weatherData = weatherData; _weatherData?.RegisterObserver(this); } public void Update(float temperature, float humidity, float pressure) { _temperature = temperature; _humidity = humidity; Display(); } public void Display() { Console.WriteLine($"Current Conditions: {_temperature}F degress and {_humidity}% humidity"); } }
3.2 Subject
public interface ISubject { void RegisterObserver(IObserver o); void RemoveObserver(IObserver o); void NotifyObservers(); }
public class WeatherData : ISubject { //观察者列表 private readonly List_observerList; //天气数据 private float _temperature; private float _humidity; private float _pressure; public WeatherData() { _observerList = new List (); } /// /// 订阅观察者 /// /// 观察者对象 public void RegisterObserver(IObserver o) { _observerList.Add(o); } ////// 移除观察者 /// /// 观察者对象 public void RemoveObserver(IObserver o) { if (_observerList.Contains(o)) { _observerList.Remove(o); } } ////// 发布数据 /// public void NotifyObservers() { foreach (var observer in _observerList) { observer.Update(_temperature, _humidity, _pressure); } } ////// 数据发生改变 /// private void MeasurementChanged() { NotifyObservers(); } ////// 更新数据 /// /// 温度 /// 湿度 /// 气压 public void SetMeasurements(float temperature, float humidity, float pressure) { _temperature = temperature; _humidity = humidity; _pressure = pressure; MeasurementChanged(); } }
3.3 测试代码
private static void TestWeatherData() { var weatherData = new WeatherData(); var currentConditionDisplay = new CurrentConditionDisplay(weatherData); weatherData.SetMeasurements(80, 65, 30.4f); weatherData.SetMeasurements(82, 70, 29.2f); weatherData.SetMeasurements(78, 90, 29.2f); }
4、使用C#中IObservable接口实现气象监测
4.1 辅助类/结构体
public struct WeatherMessage { public float Temperature { get; set; } public float Humidity { get; set; } public float Pressure { get; set; } } public class MessageUnknownException : Exception { internal MessageUnknownException() { } }
4.2 IObserver具体实现
class CurrentConditionDisplayX : IObserver, IDisplayElement { private IDisposable _unsubscribe; private float _temperature; private float _humidity; public void Subscribe(IObservable provider) { if (provider != null) { _unsubscribe = provider.Subscribe(this); } } public void Unsubscribe() { _unsubscribe?.Dispose(); _unsubscribe = null; } public void OnCompleted() { Console.WriteLine("CurrentConditionDisplayX: OnCompleted"); Unsubscribe(); } public void OnError(Exception error) { Console.WriteLine("CurrentConditionDisplayX: OnError"); } public void OnNext(WeatherMessage value) { _temperature = value.Temperature; _humidity = value.Humidity; Display(); } public void Display() { Console.WriteLine($"Current Conditions: {_temperature}F degress and {_humidity}% humidity"); } }
4.3 IObservable具体实现
public class WeatherDataX : IObservable{ private readonly List > _observerList; public WeatherDataX() { _observerList = new List >(); } /// /// 通知提供程序:某观察程序将要接收通知。 /// /// 要接收通知的对象。 ///使资源释放的观察程序的接口。 public IDisposable Subscribe(IObserverobserver) { if(!_observerList.Contains(observer)) { _observerList.Add(observer); } return new Unsubcriber(_observerList, observer); } public void SetMeasurements(Nullable message) { foreach (var observer in _observerList) { if (!message.HasValue) { observer.OnError(new MessageUnknownException()); } else { observer.OnNext(message.Value); } } } public void EndTransmission() { foreach (var observer in _observerList.ToArray()) { if (_observerList.Contains(observer)) { observer.OnCompleted(); } } _observerList.Clear(); } private class Unsubcriber : IDisposable { private List > _observerList; private IObserver _observer; public Unsubcriber(List > observerList, IObserver observer) { _observerList = observerList; _observer = observer; } public void Dispose() { if (_observerList != null && _observerList.Contains(_observer)) { _observerList.Remove(_observer); } } } }
4.4 测试代码
private static void TestWeatherDataX() { var weatherData = new WeatherDataX(); var currentConditionDisplay = new CurrentConditionDisplayX(); currentConditionDisplay.Subscribe(weatherData); weatherData.SetMeasurements(new WeatherMessage() { Temperature = 80, Humidity = 65, Pressure = 30.4f }); weatherData.SetMeasurements(new WeatherMessage() { Temperature = 82, Humidity = 70, Pressure = 29.2f }); weatherData.SetMeasurements(new WeatherMessage() { Temperature = 78, Humidity = 90, Pressure = 29.2f }); weatherData.EndTransmission(); }