观察者 (事件委托)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
观察者模式是一种常见的设计模式,它可以实现对象之间的解耦,使得一个对象的状态发生变化时,所有依赖于它的对象都能够及时得到通知并做出相应的响应。 在 C# 中,我们可以使用委托来实现观察者模式。具体实现步骤如下: 1. 定义一个委托类型,用于通知观察者对象。 ```csharp public delegate void EventHandler(object sender, EventArgs e); ``` 2. 定义一个主题类,它包含一个事件和一个触发事件的方法。 ```csharp public class Subject { public event EventHandler Notify; public void Change() { Notify?.Invoke(this, EventArgs.Empty); } } ``` 3. 定义一个观察者类,它包含一个处理事件的方法。 ```csharp public class Observer { public void Handle(object sender, EventArgs e) { Console.WriteLine("Received notification from subject."); } } ``` 4. 在主函数中创建主题对象和观察者对象,并将观察者对象注册到主题对象的事件中。 ```csharp static void Main(string[] args) { Subject subject = new Subject(); Observer observer = new Observer(); subject.Notify += observer.Handle; subject.Change(); } ``` 上述代码中,当主题对象的状态发生改变时,它会触发事件并通知所有注册到该事件中的观察者对象。观察者对象则会调用自己的处理方法来响应该事件。 以上就是使用委托实现观察者模式的基本步骤。当然,在实际应用中,我们还可以对该模式进行更加复杂的实现和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值