CarDealer类 事件发布程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateEvent { public class CarInfoEventArgs : EventArgs { public string Car { get; private set; } public CarInfoEventArgs(string car) { this.Car = car; } } /// <summary> /// 新车到达时触发的事件 /// </summary> public class CarDealer { /* * 泛型委托EventHandler<T>定义了一个处理程序,返回Void,接受两个参数 * 第一个参数必须为object,第二个参数是T类型 * T有约束,T必须派生自基类EventArgs * 即public delegate void EventHandler<TEventArgs>(object sender,TEventArgs e) * where TeventArgs:EventArgs */ public event EventHandler<CarInfoEventArgs> NewCarInfo; public void NewCar(string car) { Console.WriteLine("CarDealer ,new car {0}",car); RaiseNewCarInfo(car); } protected virtual void RaiseNewCarInfo(string car) { EventHandler<CarInfoEventArgs> newCarInfo = NewCarInfo; if (newCarInfo != null) { //事件一般使用带两个参数的方法,其中一个参数是对象包含事件的发送者 //第二个参数提供了事件的相关信息,第二个参数随不同的事件类型而不同 newCarInfo(this, new CarInfoEventArgs(car)); } } } }
Consumer类 事件监听器
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateEvent { /// <summary> /// 事件订阅类 /// </summary> class Consumer { private string name; public Consumer(string name) { this.name = name; } public void NewCarIsHere(object sender, CarInfoEventArgs e) { Console.WriteLine("{0}:car {1} is new",name,e.Car); } } }
测试类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateEvent { class Program { static void Main(string[] args) { var dealer = new CarDealer(); var michael = new Consumer("A"); //订阅者A dealer.NewCarInfo += michael.NewCarIsHere; dealer.NewCar("A_Car"); //A车到达 A得到通知 var sebastian = new Consumer("B"); //订阅者B dealer.NewCarInfo += sebastian.NewCarIsHere; dealer.NewCar("B_Car"); //B车到达 A、B都得到通知 dealer.NewCarInfo -= michael.NewCarIsHere; //A取消订阅 dealer.NewCar("C_Car"); //C车到达 只有B得到通知 } } }
测试结果
上述代码的不足体现在事件发布程序和事件监听器进行强连接。
添加弱事件管理器WeakCarInfoEventManager
using System.Windows; namespace DelegateEvent { /// <summary> /// 弱事件管理器 /// </summary> public class WeakCarInfoEventManager : WeakEventManager { /// <summary> /// 发布程序 /// </summary> /// <param name="source"></param> /// <param name="listener"></param> public static void AddListener(object source, IWeakEventListener listener) { CurrentManager.ProtectedAddListener(source, listener); } /// <summary> /// 断开发布程序 /// </summary> /// <param name="source"></param> /// <param name="listener"></param> public static void RemoveListener(object source, IWeakEventListener listener) { CurrentManager.ProtectedRemoveListener(source, listener); } public static WeakCarInfoEventManager CurrentManager { get { WeakCarInfoEventManager manager = GetCurrentManager(typeof(WeakCarInfoEventManager)) as WeakCarInfoEventManager; if (manager == null) { manager = new WeakCarInfoEventManager(); SetCurrentManager(typeof(WeakCarInfoEventManager), manager); } return manager; } } /// <summary> /// 订阅一个方法 /// </summary> /// <param name="source"></param> protected override void StartListening(object source) { (source as CarDealer).NewCarInfo += CarDealer_NewCarInfo; } void CarDealer_NewCarInfo(object sender, CarInfoEventArgs e) { DeliverEvent(sender, e); } /// <summary> /// 取消订阅一个方法 /// </summary> /// <param name="source"></param> protected override void StopListening(object source) { (source as CarDealer).NewCarInfo -= CarDealer_NewCarInfo; } } }
更正事件监听器为WeakConsumer
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace DelegateEvent { public class WeakConsumer:IWeakEventListener { private string name; public WeakConsumer(string name) { this.name = name; } public void NewCarIsHere(object sender, CarInfoEventArgs e) { Console.WriteLine("{0}: car {1} is new", name, e.Car); } bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e) { NewCarIsHere(sender, e as CarInfoEventArgs); return true; } } }
主方法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateEvent { class Program { static void Main(string[] args) { //var dealer = new CarDealer(); //var michael = new Consumer("A"); //订阅者A //dealer.NewCarInfo += michael.NewCarIsHere; //dealer.NewCar("A_Car"); //A车到达 A得到通知 //var sebastian = new Consumer("B"); //订阅者B //dealer.NewCarInfo += sebastian.NewCarIsHere; //dealer.NewCar("B_Car"); //B车到达 A、B都得到通知 //dealer.NewCarInfo -= michael.NewCarIsHere; //A取消订阅 //dealer.NewCar("C_Car"); //C车到达 只有B得到通知 #region WeakEvent var dealer = new CarDealer(); var michael = new WeakConsumer("A"); //订阅者A WeakCarInfoEventManager.AddListener(dealer,michael); dealer.NewCar("A_Car"); var sebastian = new WeakConsumer("B"); //订阅者B WeakCarInfoEventManager.AddListener(dealer, sebastian); dealer.NewCar("B_Car"); //B车到达 A、B都得到通知 WeakCarInfoEventManager.RemoveListener(dealer, michael); dealer.NewCar("C_Car"); #endregion } } }
CarDealer类 事件发布程序 不做任何更改
测试结果一致
事件发布程序和事件监听程序不再强连接
要么忍,要么狠,要么滚!