找回密码
 立即注册
首页 业界区 业界 行为型设计模式——观察者模式

行为型设计模式——观察者模式

剧拧并 2025-6-6 14:54:29
观察者模式(Observer Pattern)是一种行为型设计模式,通过定义对象间一对多的依赖关系实现状态变化的自动通知机制,广泛应用于解耦事件发布与订阅场景
适用于
①当一个抽象模型有两个方面,其中一个方面依赖于另一个方面。将这两者封装在独立地对象中以使它们可以各自独立地改变和复用。
②当对一个对象的改变需要同时改变其他对象,而不知道具体有多少对象有待改变时。
③当一个对象必须通知其他对象,而它又不能假定其他对象是谁,即:不希望这些对象是紧耦合的。
类图如下:
1.png

场景:气象局监测温度
核心代码如下:
  1. 1     internal class Program
  2. 2     {
  3. 3         static void Main(string[] args)
  4. 4         {
  5. 5             //Console.WriteLine("Hello, World!");
  6. 6             //创建具体观察者
  7. 7             DisPlay1 disPlay1 = new DisPlay1("显示屏1");
  8. 8             DisPlay2 disPlay2 = new DisPlay2("显示屏2");
  9. 9             //创建具体被观察者
  10. 10             WeatherData weatherData = new WeatherData();
  11. 11             weatherData.RedisterObserver(disPlay1);
  12. 12             weatherData.RedisterObserver(disPlay2);
  13. 13             float temperature = 25;
  14. 14             weatherData.SetTemperature(temperature);
  15. 15             Console.ReadKey();
  16. 16         }
  17. 17     }
复制代码
  1. 1     internal interface Observer
  2. 2     {
  3. 3         /// <summary>
  4. 4         /// 观察者接口
  5. 5         /// </summary>
  6. 6         /// <param name="temp">监测对象,具体场景,具体定义参数</param>
  7. 7         /// 场景:气象局监测温度
  8. 8         void Update(float temp);
  9. 9     }
复制代码
  1. 1     /// <summary>
  2. 2     /// 显示屏
  3. 3     /// </summary>
  4. 4     internal class DisPlay1 : Observer
  5. 5     {
  6. 6         private string Name { get; set; }
  7. 7         public DisPlay1(string name)
  8. 8         {
  9. 9             this.Name = name;
  10. 10         }
  11. 11         public void Update(float temp)
  12. 12         {
  13. 13             Console.WriteLine($"{Name}当前温度为{temp}摄氏度");
  14. 14         }
  15. 15     }
复制代码
  1. 1     /// <summary>
  2. 2     /// 显示屏
  3. 3     /// </summary>
  4. 4     internal class DisPlay2 : Observer
  5. 5     {
  6. 6         private string Name { get; set; }
  7. 7         public DisPlay2(string name)
  8. 8         {
  9. 9             this.Name = name;
  10. 10         }
  11. 11         public void Update(float temp)
  12. 12         {
  13. 13             Console.WriteLine($"{Name}当前温度为{temp}摄氏度");
  14. 14         }
  15. 15     }
复制代码
2.gif
3.gif
  1. 1     internal interface Subject
  2. 2     {
  3. 3         /// <summary>
  4. 4         /// 注册观察者
  5. 5         /// </summary>
  6. 6         /// <param name="observer"></param>
  7. 7         void RedisterObserver(Observer observer);
  8. 8         /// <summary>
  9. 9         /// 删除观察者
  10. 10         /// </summary>
  11. 11         /// <param name="observer"></param>
  12. 12         void RemoveObserver(Observer observer);
  13. 13         /// <summary>
  14. 14         /// 通知观察者
  15. 15         /// </summary>
  16. 16         void NoticeObserver();
  17. 17     }
复制代码
View Code
4.gif
5.gif
  1. 1  /// <summary>
  2. 2  /// 气象站
  3. 3  /// </summary>
  4. 4  internal class WeatherData : Subject
  5. 5  {
  6. 6      private List<Observer> observers;
  7. 7      private float temperature;
  8. 8
  9. 9      public WeatherData()
  10. 10      {
  11. 11          observers = new List<Observer>();
  12. 12      }
  13. 13      public void SetTemperature(float temperature)
  14. 14      {
  15. 15          this.temperature = temperature;
  16. 16          NoticeObserver();
  17. 17      }
  18. 18      public void NoticeObserver()
  19. 19      {
  20. 20          observers.ForEach(s => s.Update(temperature));
  21. 21      }
  22. 22
  23. 23      public void RedisterObserver(Observer observer)
  24. 24      {
  25. 25          //if (observers == null) { }
  26. 26          observers.Add(observer);
  27. 27      }
  28. 28
  29. 29      public void RemoveObserver(Observer observer)
  30. 30      {
  31. 31          observers.Remove(observer);
  32. 32      }
  33. 33  }
复制代码
View Code 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册