1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace Delegate 6 { 7 // 热水器 8 public class Heater 9 { 10 private int temperature; 11 public string type ="RealFire 001"; // 添加型号作为演示 12 public string area = "China Xian"; // 添加产地作为演示 13 //声明委托 14 public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e); 15 public event BoiledEventHandler Boiled;//声明事件 16 17 // 定义BoiledEventArgs类,传递给Observer所感兴趣的信息 18 public class BoiledEventArgs : EventArgs 19 { 20 public readonly int temperature; 21 public BoiledEventArgs(int temperature) 22 { 23 this.temperature = temperature; 24 } 25 } 26 27 // 可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视 28 protected virtual void OnBoiled(BoiledEventArgs e) 29 { 30 if (Boiled != null) 31 { // 如果有对象注册 32 Boiled(this, e); // 调用所有注册对象的方法 33 } 34 } 35 36 // 烧水。 37 public void BoilWater() 38 { 39 for (int i = 0; i <= 10; i++) 40 { 41 temperature = i; 42 if (temperature > 5) 43 { 44 //建立BoiledEventArgs 对象。 45 BoiledEventArgs e = new BoiledEventArgs(temperature); 46 OnBoiled(e); // 调用 OnBolied方法 47 } 48 } 49 } 50 } 51 52 // 警报器 53 public class Alarm 54 { 55 public void MakeAlert(Object sender, Heater.BoiledEventArgs e) 56 { 57 Heater heater = (Heater)sender; //这里是不是很熟悉呢? 58 //访问 sender 中的公共字段 59 Console.WriteLine("Alarm:{0} - {1}: ", heater.area, heater.type); 60 Console.WriteLine("Alarm: 嘀嘀嘀,水已经 {0} 度了:", e.temperature); 61 Console.WriteLine(); 62 } 63 } 64 65 // 显示器 66 public class Display 67 { 68 public static void ShowMsg(Object sender, Heater.BoiledEventArgs e) 69 { //静态方法 70 Heater heater = (Heater)sender; 71 Console.WriteLine("Display:{0} - {1}: ", heater.area, heater.type); 72 Console.WriteLine("Display:水快烧开了,当前温度:{0}度。", e.temperature); 73 Console.WriteLine(); 74 } 75 } 76 77 class Program 78 { 79 static void Main() 80 { 81 Heater heater = new Heater(); 82 Alarm alarm = new Alarm(); 83 84 heater.Boiled += alarm.MakeAlert; //注册方法 85 heater.Boiled += (new Alarm()).MakeAlert; //给匿名对象注册方法 86 heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert); //也可以这么注册 87 heater.Boiled += Display.ShowMsg; //注册静态方法 88 89 heater.BoilWater(); //烧水,会自动调用注册过对象的方法 90 Console.ReadKey(); 91 } 92 } 93 }
另一个例子
1 using System; 2 //步骤1:创建代理对象 3 public delegate void MyHandler1(object sender, MyEventArgs e); 4 public delegate void MyHandler2(object sender, MyEventArgs e); 5 //步骤2:创建事件处理程序方法 6 class A 7 { 8 //public const string m_id = "Class A"; 9 public void OnHandler1(object sender, MyEventArgs e) 10 { 11 Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}", e.m_id); 12 } 13 public void OnHandler2(object sender, MyEventArgs e) 14 { 15 Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}", e.m_id); 16 } 17 //步骤3:创建代理,安装事件处理程序,并向启动事件的对象注册。 18 public A(B b) 19 { 20 MyHandler1 d1 = new MyHandler1(OnHandler1); 21 MyHandler2 d2 = new MyHandler2(OnHandler2); 22 b.Event1 += d1; 23 b.Event2 += d2; 24 } 25 } 26 //步骤4:通过代理调用封装的方法。 27 class B 28 { 29 public event MyHandler1 Event1; 30 public event MyHandler2 Event2; 31 public void FireEvent1(MyEventArgs e) 32 { 33 if (Event1 != null) 34 { 35 Event1(this, e); 36 } 37 } 38 public void FireEvent2(MyEventArgs e) 39 { 40 if (Event2 != null) 41 { 42 Event2(this, e); 43 } 44 } 45 } 46 public class MyEventArgs : EventArgs 47 { 48 public string m_id; 49 } 50 public class Driver 51 { 52 public static void Main() 53 { 54 B b = new B(); 55 A a = new A(b); 56 MyEventArgs e1 = new MyEventArgs(); 57 MyEventArgs e2 = new MyEventArgs(); 58 e1.m_id = "Event args for event 1"; 59 e2.m_id = "Event args for event 2"; 60 b.FireEvent1(e1); 61 b.FireEvent2(e2); 62 Console.ReadKey(); 63 } 64 }
原文:http://www.cnblogs.com/moon-night/p/3633343.html