观察者 (事件委托)

来源:互联网 发布:运营数据分析 数据挖掘 编辑:程序博客网 时间:2024/06/05 11:04
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


//委托事件
public class Notifications
{
    private static Notifications _event;


    public static Notifications Event
    {
        get
        {
            return _event ?? (_event = new Notifications());
        }
    }


    public class Args : System.EventArgs { }


    private static Dictionary<string, List<Pair>> eventsDic = new Dictionary<string, List<Pair>>();//事件集合
    public delegate void StandardDelegate(Args e);


    class Pair
    {
        public object obj;
        public StandardDelegate deleget;


        public Pair(object obj, StandardDelegate d)
        {
            this.obj = obj;
            this.deleget = d;
        }
    }


    /// <summary>
    /// 注册事件
    /// </summary>
    /// <param name="eventName">事件名</param>
    /// <param name="obj"></param>
    /// <param name="d">时间委托</param>
    public void Register(string eventName, object obj, StandardDelegate d)
    {
        //判断是否存在该事件
        if (eventsDic.ContainsKey(eventName))
        {
            foreach (Pair pair in eventsDic[eventName])
            {
                if (pair.Equals(obj))
                {
                    pair.deleget += d;
                    return;
                }
            }
            eventsDic[eventName].Add(new Pair(obj, d));
        }
        else
        {
            List<Pair> pList = new List<Pair>();
            pList.Add(new Pair(obj, d));
            eventsDic.Add(eventName, pList);
        }
    }


    /// <summary>
    /// 取消事件
    /// </summary>
    /// <param name="obj"></param>
    public void DeRegister(object obj)
    {
        List<string> funcNameList = new List<string>();


        foreach (KeyValuePair<string, List<Pair>> kv in eventsDic)
        {
            var values = kv.Value;
            for (int i = 0; i < values.Count; i++)
            {
                if (values[i].obj.Equals(obj))
                {
                    values.RemoveAt(i);
                    continue;
                }
                if (values.Count == 0)
                {
                    funcNameList.Add(kv.Key);
                }


            }


            //从字典中移除List<Pair>为空的键值
            foreach (string s in funcNameList)
            {
                if (eventsDic.ContainsKey(s))
                {
                    eventsDic.Remove(s);
                }
            }
        }
    }


    /// <summary>
    /// 调用时间
    /// </summary>
    /// <param name="eventName"></param>
    /// <param name="e"></param>
    public void Fire(string eventName, Args e)
    {
        if (eventsDic.ContainsKey(eventName))
        {
            foreach (Pair pair in eventsDic[eventName])
            {
                if (pair.obj != null && pair.deleget != null)
                {
                    pair.deleget(e);
                }
            }
        }
        else
        {
            Debug.LogError("没有注册过该事件");
        }
    }
}
0 0
原创粉丝点击