基于Delegate 实现的事件派发管理系统

1.先定义事件参数类: EventArgs 是基类,不建议直接使用,因为通用的东西针对性不强,容易混乱(特别是找bug 的时候)... MyEvent 的具体的派生类,建议的使用方法就是每个种类的事件派生一个类,比如UIEvent,ServerEvent神马的,根据具体作用来命名.

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 事件参数基类  
  3. /// </summary>  
  4. public class EventArgs  
  5. {  
  6.     public object Parameter;  
  7. }  
  8.   
  9. /// <summary>  
  10. /// 自定义事件参数  
  11. /// </summary>  
  12. public class MyEvent : EventArgs  
  13. {  
  14.     public int ID;  
  15.     public string Name; // ...etc   
  16. }  


2. 事件管理类: 可以看出是一个单例类,世界各地都能直接调用. _delegates负责保存所有事件接收方法,事件被按类型存在里面,同一个类型无论几条记录都只占一个项(所以不用遍历大串的方法列表)

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 事件管理类  
  3. /// </summary>  
  4. public class EventManager  
  5. {  
  6.     //单例模式.  
  7.     public static readonly EventManager Instance = new EventManager();  
  8.     private EventManager() { }  
  9.   
  10.     //事件委托.  
  11.     public delegate void EventDelegate<T>(T e) where T : EventArgs;  
  12.   
  13.     //保存所有事件<span style="font-family:Arial,Helvetica,sans-serif">接收方法</span>.  
  14.     readonly Dictionary<Type, Delegate> _delegates = new Dictionary<Type, Delegate>();  
  15.   
  16.     //添加一个事件接收方法.  
  17.     public void AddListener<T>(EventDelegate<T> listener) where T : EventArgs  
  18.     {  
  19.         Delegate d;  
  20.         if (_delegates.TryGetValue(typeof(T), out d))  
  21.         {  
  22.             _delegates[typeof(T)] = Delegate.Combine(d, listener);  
  23.         }  
  24.         else  
  25.         {  
  26.             _delegates[typeof(T)] = listener;  
  27.         }  
  28.     }  
  29.   
  30.     //删除一个事件接受方法  
  31.     public void RemoveListener<T>(EventDelegate<T> listener) where T : EventArgs  
  32.     {  
  33.         Delegate d;  
  34.         if (_delegates.TryGetValue(typeof(T), out d))  
  35.         {  
  36.             Delegate currentDel = Delegate.Remove(d, listener);  
  37.   
  38.             if (currentDel == null)  
  39.             {  
  40.                 _delegates.Remove(typeof(T));  
  41.             }  
  42.             else  
  43.             {  
  44.                 _delegates[typeof(T)] = currentDel;  
  45.             }  
  46.         }  
  47.     }  
  48.   
  49.     //发送事件.  
  50.     public void Send<T>(T e) where T : EventArgs  
  51.     {  
  52.         if (e == null)  
  53.         {  
  54.             throw new ArgumentNullException("e");  
  55.         }  
  56.   
  57.         Delegate d;  
  58.         if (_delegates.TryGetValue(typeof(T), out d))  
  59.         {  
  60.             EventDelegate<T> callback = d as EventDelegate<T>;  
  61.             if (callback != null)  
  62.             {  
  63.                 callback(e);  
  64.             }  
  65.         }  
  66.     }  
  67. }  

3. 使用示例: 使用灰常简单,如下: 先添加一个事件接收方法(就是当事件被发出的时候会调用的接收方法), 然后需要触发事件时使用Send方法发送即可.

[csharp]  view plain  copy
  1. /// <summary>  
  2. /// 使用示例  
  3. /// </summary>  
  4. public class Test  
  5. {  
  6.     public Test()  
  7.     {  
  8.         //添加事件接收方法  
  9.         EventManager.Instance.AddListener<MyEvent>(ReceiveEvent);  
  10.   
  11.   
  12.         //发送MyEvent事件, 之后ReceiveEvent(MyEvent e)方法将被调用.  
  13.         MyEvent e = new MyEvent(); //事件参数.  
  14.         e.ID =0;  
  15.         e.Name = "XOXOOX";  
  16.         //事件发送  
  17.         EventManager.Instance.Send<MyEvent>(e);  
  18.     }  
  19.   
  20.     //接收MyEvent事件的方法.  
  21.     public void ReceiveEvent(MyEvent e)  
  22.     {  
  23.           
  24.     }  
  25.   
  26. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值