• C# 设计模式 观察者模式示例


    请添加图片描述

    1.观察者(订阅者)接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Observer
    {
        /// 
        /// 观察者接口
        /// 
        public interface IObserver
        {
            /// 
            /// 观察到事件 调用函数
            /// 
            void Update();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.主题抽象类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Observer
    {
        /// 
        /// 主题
        /// 
        public class Subject
        {
            // 观察者列表
            private List<IObserver> observers = new List<IObserver>();
    
            /// 
            /// 添加观察者
            /// 
            /// 
            public void AddObserver(IObserver observer)
            {
                // 添加订阅者
                Console.WriteLine("----添加订阅者---");
                this.observers.Add(observer);
            }
    
            /// 
            /// 删除观察者
            /// 
            /// 
            public void DeleteObserver(IObserver observer)
            {
                // 删除订阅者
                this.observers.Remove(observer);
            }
    
            // 通知所有观察者
            public void NotifyObservers()
            {
                foreach (IObserver item in observers)
                {
                    item.Update();
                }
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    3.具体主题类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Observer
    {
        /// 
        /// 老板动作主题 发布者
        /// 
        public class BossConcreateSubject : Subject
        {
    
            public void DoSomething()
            {
                Console.WriteLine("......老板做些事情..........");
                Console.WriteLine("......老板视察工作..........");
    
                // 发布事件 通知订阅者
                this.NotifyObservers();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    4、观察者A

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Observer
    {
        public class ConcreateObserverA : IObserver
        {
            public void Update()
            {
                Console.WriteLine("观察者A -- 接受到事件");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5、观察者B

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Observer
    {
        public class ConcreateObserverB : IObserver
        {
            public void Update()
            {
                Console.WriteLine("观察者B -- 接受到事件");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    6、测试观察者模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Observer
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("---------------------观察者模式-----------------------");
                // 创建主题
                BossConcreateSubject bossConcreateSubject = new BossConcreateSubject();
    
                // 创建观察者
                ConcreateObserverA observerA = new ConcreateObserverA();
                ConcreateObserverB observerB = new ConcreateObserverB();
    
                Console.WriteLine("---------------------主题添加观察者-----------------------");
                bossConcreateSubject.AddObserver(observerA);
                bossConcreateSubject.AddObserver(observerB);
    
                Console.WriteLine("---------------------主题触发事件-----------------------");
                bossConcreateSubject.DoSomething();
    
                Console.ReadLine();
    
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    显示结果

    在这里插入图片描述

    优缺点:

    优点:
    1、降低目标(主题)与观察者之间的耦合关系、主题与观察者建立了触发机制。
    2、观察者与被观察者都是 抽象耦合的。

    缺点:
    1、观察者与观察目标有循环依赖关系、触发它们之间的循环调用、可能会导致系统崩溃
    2、因为触发是循环调用,如果观察者过多会花费很长时间。

  • 相关阅读:
    2022年9月起ios真机无法运行标准基座
    css3中有哪些新属性(特性)?
    笔训day1
    RabbitMQ的使用
    git使用及常用命令
    如何在 uniapp 里面使用 pinia 数据持久化 (pinia-plugin-persistedstate)
    【排序算法】希尔排序
    【嵌入式DIY实例】-最大功率点跟踪 (MPPT) 太阳能充电控制器
    【NOWCODER】- Python:内置函数(三)
    linux格式化输入输出
  • 原文地址:https://blog.csdn.net/weixin_45875105/article/details/126155439