您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关怎么在c#项目中实现一个观察者模式,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
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<IObserver> _observerList; //天气数据 private float _temperature; private float _humidity; private float _pressure; public WeatherData() { _observerList = new List<IObserver>(); } /// <summary> /// 订阅观察者 /// </summary> /// <param name="o">观察者对象</param> public void RegisterObserver(IObserver o) { _observerList.Add(o); } /// <summary> /// 移除观察者 /// </summary> /// <param name="o">观察者对象</param> public void RemoveObserver(IObserver o) { if (_observerList.Contains(o)) { _observerList.Remove(o); } } /// <summary> /// 发布数据 /// </summary> public void NotifyObservers() { foreach (var observer in _observerList) { observer.Update(_temperature, _humidity, _pressure); } } /// <summary> /// 数据发生改变 /// </summary> private void MeasurementChanged() { NotifyObservers(); } /// <summary> /// 更新数据 /// </summary> /// <param name="temperature">温度</param> /// <param name="humidity">湿度</param> /// <param name="pressure">气压</param> 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<WeatherMessage>, IDisplayElement { private IDisposable _unsubscribe; private float _temperature; private float _humidity; public void Subscribe(IObservable<WeatherMessage> 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<WeatherMessage> { private readonly List<IObserver<WeatherMessage>> _observerList; public WeatherDataX() { _observerList = new List<IObserver<WeatherMessage>>(); } /// <summary> /// 通知提供程序:某观察程序将要接收通知。 /// </summary> /// <param name="observer">要接收通知的对象。</param> /// <returns>使资源释放的观察程序的接口。</returns> public IDisposable Subscribe(IObserver<WeatherMessage> observer) { if(!_observerList.Contains(observer)) { _observerList.Add(observer); } return new Unsubcriber(_observerList, observer); } public void SetMeasurements(Nullable<WeatherMessage> 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<IObserver<WeatherMessage>> _observerList; private IObserver<WeatherMessage> _observer; public Unsubcriber(List<IObserver<WeatherMessage>> observerList, IObserver<WeatherMessage> 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#项目中实现一个观察者模式就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。