基於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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章