C# 观察者模式实现

代码:

cs 复制代码
using System;
using System.Collections.Generic;

public delegate void NotificationObserverCallback(object obj);

public class NotificationObserver
{
    public NotificationObserverCallback Selector = null;
    public string KeyName = "";
}

public class NotificationCenter{

    private static NotificationCenter _instance;

    private Dictionary<object, List<NotificationObserver>> _allObserversDic;

    public static NotificationCenter Instance
    {
        get { if (_instance == null) { _instance = new NotificationCenter(); } return _instance; }
    }

    private NotificationCenter()
    {
        _allObserversDic = new Dictionary<object, List<NotificationObserver>>();
    }

    private bool ObserverExisted(object target,  string name)
    {
        if (target != null) 
        {
            if (_allObserversDic.ContainsKey(target))
            {
                var lists = _allObserversDic[target];
                foreach(var item in lists)
                {
                    if (item.KeyName == name)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public void AddObserver(object target, NotificationObserverCallback selector, string name)
    {
        if (target != null && name.Length > 0) 
        {
            if (this.ObserverExisted(target, name) == false)
            {
                var observer = new NotificationObserver();
                observer.Selector = selector;
                observer.KeyName = name;

                if (_allObserversDic.ContainsKey(target) == false)
                {
                    var lists = new List<NotificationObserver>();
                    lists.Add(observer);
                    _allObserversDic.Add(target, lists);
                }
                else
                {
                    _allObserversDic[target].Add(observer);
                }
            }
        }
    }

    public void RemoveObserver(object target, string name)
    {
        if (_allObserversDic.ContainsKey(target))
        {
            var lists = _allObserversDic[target];
            foreach (var item in lists)
            {
                if (item.KeyName == name)
                {
                    lists.Remove(item);
                    break;
                }
            }

            if (lists.Count == 0)
            {
                _allObserversDic.Remove(target);
            }
        }
    }

    public void RemoveAllObservers(object target)
    {
        _allObserversDic.Remove(target);
    }

    private void TryPostNotification(string name,object obj)
    {
        foreach (var pair in _allObserversDic)
        {
            foreach (var item in pair.Value)
            {
                if (item.KeyName == name) 
                {
                    if (item.Selector != null) 
                    {
                        item.Selector(obj);
                    }
                }
            }
        }
    }

    public void PostNotification(string name)
    {
        this.TryPostNotification(name, null);
    }
}

添加监听

cs 复制代码
public void Init()
{
    NotificationCenter.Instance.AddObserver(this, TestRespond, "test__");
}

private void TestRespond(object obj) {
     
}

抛出事件

cs 复制代码
NotificationCenter.Instance.PostNotification("test__");
相关推荐
晨星shine5 小时前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户2986985301413 小时前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户36674625267414 小时前
接口文档汇总 - 2.设备状态管理
c#
用户36674625267414 小时前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf4 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530145 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools6 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的6 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21886 天前
.NET 本地Db数据库-技术方案选型
windows·c#