大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关怎么在c#项目中实现一个观察者模式,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
迪庆州网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联建站从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。1、观察者模式UML图
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.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(); }
关于怎么在c#项目中实现一个观察者模式就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。