委托的前世今生

 1 namespace Heater
 2 {
 3     //热水器
 4     public class Heater{
 5         private int temperature; //水温
 6         public delegate void BoilHander(int param);
 7         public event BoilHander BoilEvent;
 8 
 9         // 烧水
10         public void BoilWater() {
11             for (int i = 0; i <= 100; i++) {
12                 temperature = i;
13 
14                 if (temperature > 95) {
15                     if (BoilEvent != null)
16                     {
17                         BoilEvent(temperature);
18                     }
19                 }
20             }
21         }
22     }
23    
24     public class Alarm{
25         //发出语音警报
26         public void MakeAlert(int param) {
27             Console.WriteLine("Alarm: 嘀嘀嘀,水已经{0}度了:",param);
28         }
29     }
30 
31     public class Display {
32         //显示水温
33         public static void ShowMsg(int param)
34         {
35             Console.WriteLine("Alarm: 水快开了,当前温度{0}度:", param);
36         }
37     }
38 
39     class Program
40     {
41         static void Main(string[] args)
42         {
43             Heater heater = new Heater();
44             Alarm alarm = new Alarm();
45 
46             heater.BoilEvent += alarm.MakeAlert;
47             heater.BoilEvent += (new Alarm()).MakeAlert;
48             heater.BoilEvent += Display.ShowMsg;
49             heater.BoilWater();
50             Console.ReadLine();
51         }
52     }
53 }
54 
.Net Framework规范
 1 
 2 namespace Heater
 3 {
 4     //热水器
 5     public class Heater{
 6         private int temperature; //水温
 7         public string type = "RealFire001";  //添加型号作为演示
 8         public string area = "China XiAn";  //添加产地作为演示
 9         //声明委托
10         public delegate void BoiledEventHander(Object sender,BoiledEventArgs e);
11         public event BoiledEventHander Boiled;
12 
13         //定义BoiledEventArgs类,传递给Object所感兴趣的信息
14         public class BoiledEventArgs : EventArgs {
15             public readonly int temperature;
16             public BoiledEventArgs(int temperature) {
17                 this.temperature = temperature;
18             }
19         }
20 
21         //可以供继承自Heater的类重写,以便继承类拒绝其他对象对它的监视
22         protected virtual void OnBoiled(BoiledEventArgs e) {
23             if (Boiled != null) {
24                 Boiled(this,e);
25             }
26         }
27         // 烧水
28         public void BoilWater() {
29             for (int i = 0; i <= 100; i++) {
30                 temperature = i;
31 
32                 if (temperature > 95) {
33                     BoiledEventArgs e = new BoiledEventArgs(temperature);
34                     OnBoiled(e);
35                 }
36             }
37         }
38     }
39    
40     //警报器
41     public class Alarm{
42         //发出语音警报
43         public void MakeAlert(Object sender,Heater.BoiledEventArgs e) {
44             Heater heater = (Heater)sender;
45             Console.WriteLine("Alarm:{0}-{1}:",heater.area,heater.type);
46             Console.WriteLine("Alarm: 嘀嘀嘀,水已经{0}度了:",e.temperature);
47             Console.WriteLine();
48         }
49     }
50 
51     public class Display {
52         //显示水温
53         public static void ShowMsg(Object sender,Heater.BoiledEventArgs e)
54         {
55             Heater heater = (Heater)sender;
56             Console.WriteLine("Display:{0}-{1}:",heater.area,heater.type);
57             Console.WriteLine("Alarm: 水快开了,当前温度{0}度:", e.temperature);
58         }
59     }
60 
61     class Program
62     {
63         static void Main(string[] args)
64         {
65             Heater heater = new Heater();
66             Alarm alarm = new Alarm();
67 
68             heater.Boiled += alarm.MakeAlert;
69             heater.Boiled += new Heater.BoiledEventHander(alarm.MakeAlert);
70             heater.Boiled += Display.ShowMsg;
71             heater.BoilWater();
72             Console.ReadLine();
73         }
74     }
75 }
76 

 

 

posted @ 2009-12-22 17:52  风影极光  阅读(248)  评论(0编辑  收藏  举报