用于模块间解耦,通过发布订阅的方式调用,每个人只负责自己的那部分。
写个小例子,比如现在有三个模块,订单、购物车、优惠券,由不同的人负责开发。
负责订单模块的人现在需要写个生成订单的方法,生成订单的逻辑包括删除购物车项、标记优惠券已使用。
如果直接调用购物车、优惠券两个模块中的方法,耦合性高。
此时可以使用EventBus进行解耦。
1 public class EventBus 2 { 3 private EventBus() { } 4 private static Object syncObj = new object(); 5 private static EventBus _instance = null; 6 public static EventBus Instance 7 { 8 get 9 { 10 if (_instance == null) 11 { 12 lock (syncObj) 13 { 14 if (_instance == null) 15 { 16 _instance = new EventBus(); 17 } 18 } 19 } 20 return _instance; 21 } 22 } 23 24 private ConcurrentDictionary<string, List<Action<object[]>>> _dict = new ConcurrentDictionary<string, List<Action<object[]>>>(); 25 26 private bool ExistEvent(string eventName) 27 { 28 if (_dict.ContainsKey(eventName)) 29 { 30 return true; 31 } 32 return false; 33 } 34 public void InvokeEvent(string eventName,object[] args) 35 { 36 if (!this.ExistEvent(eventName)) 37 { 38 return; 39 } 40 foreach (var eventHandler in this._dict[eventName]) 41 { 42 eventHandler.Invoke(args); 43 } 44 } 45 public void AddEvent(string eventName, Action<object[]> eventHandler) 46 { 47 if (this.ExistEvent(eventName)) 48 { 49 this._dict[eventName].Add(eventHandler); 50 } 51 else { 52 this._dict[eventName] = new List<Action<object[]>> { eventHandler }; 53 } 54 } 55 public void AddEvent(string eventName, List<Action<object[]>> eventHandlerList) 56 { 57 if (this.ExistEvent(eventName)) 58 { 59 this._dict[eventName].AddRange(eventHandlerList); 60 } 61 else 62 { 63 this._dict[eventName] = eventHandlerList; 64 } 65 } 66 public void RemoveEvent(string eventName) 67 { 68 if (this.ExistEvent(eventName)) 69 { 70 this._dict.TryRemove(eventName,out var value); 71 } 72 } 73 public void RemoveEvent(string eventName, Action<object[]> eventHandler) 74 { 75 if (this.ExistEvent(eventName)) 76 { 77 this._dict[eventName].Remove(eventHandler); 78 } 79 } 80 81 }
1 public class ShopCartService 2 { 3 /// <summary> 4 /// 删除购物车 5 /// </summary> 6 /// <param name="shopCartID"></param> 7 public void RemoveShopCart(int orderID) 8 { 9 Console.WriteLine($"已删除订单{orderID}的购物车项"); 10 } 11 } 12 public class CouponService 13 { 14 /// <summary> 15 /// 标记优惠券已使用 16 /// </summary> 17 public void RecordUsedCoupon(int orderID) 18 { 19 Console.WriteLine($"已将订单{orderID}标记已使用优惠券"); 20 } 21 } 22 public class OrderService 23 { 24 /// <summary> 25 /// 创建订单 26 /// </summary> 27 public void CreateOrder() 28 { 29 int orderID =100000; 30 Console.WriteLine($"生成订单"); 31 EventBus.Instance.InvokeEvent(EventNames.CREATEORDER_EVENT, new object[] { orderID }); 32 } 33 }
public static class EventNames { public static string CREATEORDER_EVENT = "CreateOrder"; }
---------------------------------------------------------------------------
订阅、发布
1 static void Main(string[] args) 2 { 3 //订阅 4 EventBus.Instance.AddEvent(EventNames.CREATEORDER_EVENT, new List<Action<object[]>> 5 { 6 (args)=>{ new ShopCartService().RemoveShopCart(Int32.Parse(args[0].ToString())); }, 7 (args)=>{ new CouponService().RecordUsedCoupon(Int32.Parse(args[0].ToString())); } 8 }); 9 10 11 var orderService = new OrderService(); 12 orderService.CreateOrder(); 13 14 }