C# 观察者模式实现

时间:2024-05-31 12:57:45

代码:


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);
    }
}


 添加监听

public void Init()
{
    NotificationCenter.Instance.AddObserver(this, TestRespond, "test__");
}

private void TestRespond(object obj) {
     
}

 抛出事件

NotificationCenter.Instance.PostNotification("test__");