1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //连接发布者和订阅者
6 Thermostat tm = new Thermostat(); 7 Cooler cl = new Cooler(40); 8 Heater ht = new Heater(60); 9 //设置委托变量关联的方法。+=可以存储多个方法,这些方法称为订阅者。
10 tm.OnTemperatureChange += cl.OnTemperatureChanged; 11 tm.OnTemperatureChange += ht.OnTemperatureChanged; 12 string temperature = Console.ReadLine(); 13
14 //将数据发布给订阅者(本质是依次运行那些方法)
15 tm.OnTemperatureChange(float.Parse(temperature)); 16
17 Console.ReadLine(); 18
19
20
21 } 22 } 23 //两个订阅者类
24 class Cooler 25 { 26 public Cooler(float temperature) 27 { 28 _Temperature = temperature; 29 } 30 private float _Temperature; 31 public float Temperature 32 { 33 set
34 { 35 _Temperature = value; 36 } 37 get
38 { 39 return _Temperature; 40 } 41 } 42
43 //将来会用作委托变量使用,也称为订阅者方法
44 public void OnTemperatureChanged(float newTemperature) 45 { 46 if (newTemperature > _Temperature) 47 { 48 Console.WriteLine("Cooler:on ! "); 49 } 50 else
51 { 52 Console.WriteLine("Cooler:off ! "); 53 } 54 } 55 } 56 class Heater 57 { 58 public Heater(float temperature) 59 { 60 _Temperature = temperature; 61 } 62 private float _Temperature; 63 public float Temperature 64 { 65 set
66 { 67 _Temperature = value; 68 } 69 get
70 { 71 return _Temperature; 72 } 73 } 74 public void OnTemperatureChanged(float newTemperature) 75 { 76 if (newTemperature < _Temperature) 77 { 78 Console.WriteLine("Heater:on ! "); 79 } 80 else
81 { 82 Console.WriteLine("Heater:off ! "); 83 } 84 } 85 } 86
87
88 //发布者
89 class Thermostat 90 { 91
92 //定义一个委托类型
93 public delegate void TemperatureChangeHanlder(float newTemperature); 94 //定义一个委托类型变量,用来存储订阅者列表。注:只需一个委托字段就可以存储所有订阅者。
95 private TemperatureChangeHanlder _OnTemperatureChange; 96 //现在的温度
97 private float _CurrentTemperature; 98
99 public TemperatureChangeHanlder OnTemperatureChange 100 { 101 set { _OnTemperatureChange = value; } 102 get { return _OnTemperatureChange; } 103 } 104
105
106 public float CurrentTemperature 107 { 108 get { return _CurrentTemperature;} 109 set
110 { 111 if (value != _CurrentTemperature) 112 { 113 _CurrentTemperature = value; 114 } 115 } 116 } 117 }
public float CurrentTemperature { get { return _CurrentTemperature; } set { if (value != _CurrentTemperature) { _CurrentTemperature = value; OnTemperatureChange(value); } } }
public float CurrentTemperature { get { return _CurrentTemperature; } set { if (value != _CurrentTemperature) { _CurrentTemperature = value; TemperatureChangeHanlder localOnChange = OnTemperatureChange; if (localOnChange != null) { //OnTemperatureChange = null;
localOnChange(value); } } } }
1 Thermostat.TemperatureChangeHanlder delegate1; 2 Thermostat.TemperatureChangeHanlder delegate2; 3 Thermostat.TemperatureChangeHanlder delegate3; 4 delegate3 = tm.OnTemperatureChange; 5 delegate1 = cl.OnTemperatureChanged; 6 delegate2 = ht.OnTemperatureChanged; 7 delegate3 += delegate1; 8 delegate3 += delegate2;
1 Thermostat.TemperatureChangeHanlder delegate1; 2 Thermostat.TemperatureChangeHanlder delegate2; 3 Thermostat.TemperatureChangeHanlder delegate3; 4 delegate1 = cl.OnTemperatureChanged; 5 delegate2 = ht.OnTemperatureChanged; 6 delegate3 = delegate1 + delegate2; 7 delegate3 = delegate3 - delegate2; 8 tm.OnTemperatureChange = delegate3;
1 public float CurrentTemperature 2 { 3 get { return _CurrentTemperature; } 4 set
5 { 6 if (value != _CurrentTemperature) 7 { 8
9 _CurrentTemperature = value; 10 TemperatureChangeHanlder localOnChange = OnTemperatureChange; 11 if (localOnChange != null) 12 { 13 foreach (TemperatureChangeHanlder hanlder in localOnChange.GetInvocationList()) 14 { 15 try
16 { 17 hanlder(value); 18 } 19 catch (Exception e) 20 { 21 Console.WriteLine(e.Message); 22
23 } 24 } 25 } 26
27 } 28 } 29 }
1 public class Thermostat 2 { 3 private float _CurrentTemperature; 4 public float CurrentTemperature 5 { 6 set { _CurrentTemperature = value; } 7 get { return _CurrentTemperature; } 8 } 9 //定义委托类型
10 public delegate void TemperatureChangeHandler(object sender, TemperatureArgs newTemperatrue); 11
12 //定义一个委托变量,并用event修饰,被修饰后有一个新的名字,事件发布者。
13 public event TemperatureChangeHandler OnTemperatureChange = delegate { }; 14
15
16 public class TemperatureArgs : System.EventArgs 17 { 18 private float _newTemperature; 19 public float NewTemperature 20 { 21 set { _newTemperature = value; } 22 get { return _newTemperature; } 23 } 24 public TemperatureArgs(float newTemperature) 25 { 26 _newTemperature = newTemperature; 27 } 28
29 } 30 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Thermostat tm = new Thermostat(); 6
7 Cooler cl = new Cooler(40); 8 Heater ht = new Heater(60); 9
10 //设置订阅者(方法)
11 tm.OnTemperatureChange += cl.OnTemperatureChanged; 12 tm.OnTemperatureChange += ht.OnTemperatureChanged; 13
14 tm.CurrentTemperature = 100; 15 } 16 } 17 //发布者类
18 public class Thermostat 19 { 20 private float _CurrentTemperature; 21 public float CurrentTemperature 22 { 23 set
24 { 25 if (value != _CurrentTemperature) 26 { 27 _CurrentTemperature = value; 28 if (OnTemperatureChange != null) 29 { 30 OnTemperatureChange(this, new TemperatureArgs(value)); 31 } 32
33 } 34 } 35 get { return _CurrentTemperature; } 36 } 37 //定义委托类型
38 public delegate void TemperatureChangeHandler(object sender, TemperatureArgs newTemperatrue); 39
40 //定义一个委托变量,并用event修饰,被修饰后有一个新的名字,事件发布者。
41 public event TemperatureChangeHandler OnTemperatureChange = delegate { }; 42
43 //用来给事件传递的数据类型
44 public class TemperatureArgs : System.EventArgs 45 { 46 private float _newTemperature; 47 public float NewTemperature 48 { 49 set { _newTemperature = value; } 50 get { return _newTemperature; } 51 } 52 public TemperatureArgs(float newTemperature) 53 { 54 _newTemperature = newTemperature; 55 } 56
57 } 58 } 59
60 //两个订阅者类
61 class Cooler 62 { 63 public Cooler(float temperature) 64 { 65 _Temperature = temperature; 66 } 67 private float _Temperature; 68 public float Temperature 69 { 70 set
71 { 72 _Temperature = value; 73 } 74 get
75 { 76 return _Temperature; 77 } 78 } 79
80 //将来会用作委托变量使用,也称为订阅者方法
81 public void OnTemperatureChanged(object sender, Thermostat.TemperatureArgs newTemperature) 82 { 83 if (newTemperature.NewTemperature > _Temperature) 84 { 85 Console.WriteLine("Cooler:on ! "); 86 } 87 else
88 { 89 Console.WriteLine("Cooler:off ! "); 90 } 91 } 92 } 93 class Heater 94 { 95 public Heater(float temperature) 96 { 97 _Temperature = temperature; 98 } 99 private float _Temperature; 100 public float Temperature 101 { 102 set
103 { 104 _Temperature = value; 105 } 106 get
107 { 108 return _Temperature; 109 } 110 } 111 public void OnTemperatureChanged(object sender, Thermostat.TemperatureArgs newTemperature) 112 { 113 if (newTemperature.NewTemperature < _Temperature) 114 { 115 Console.WriteLine("Heater:on ! "); 116 } 117 else
118 { 119 Console.WriteLine("Heater:off ! "); 120 } 121 } 122 }
1 //定义委托类型
2 public delegate void TemperatureChangeHandler(object sender, TemperatureArgs newTemperatrue); 3
4 //定义一个委托变量,并用event修饰,被修饰后有一个新的名字,事件发布者。
5 public event TemperatureChangeHandler OnTemperatureChange = delegate { }; 6
7 在编译器的作用下,会自动扩展成: 8 private TemperatureChangeHandler _OnTemperatureChange = delegate { }; 9
10 public void add_OnTemperatureChange(TemperatureChangeHandler handler) 11 { 12 Delegate.Combine(_OnTemperatureChange, handler); 13 } 14 public void remove_OnTemperatureChange(TemperatureChangeHandler handler) 15 { 16 Delegate.Remove(_OnTemperatureChange, handler); 17 } 18 public event TemperatureChangeHandler OnTemperatureChange 19 { 20 add 21 { 22 add_OnTemperatureChange(value); 23 } 24
25 remove 26 { 27 remove_OnTemperatureChange(value); 28 } 29
30 }
1 protected TemperatureChangeHandler _OnTemperatureChange = delegate { }; 2
3 public event TemperatureChangeHandler OnTemperatureChange 4 { 5 add 6 { 7 //此处代码可以自定义
8 Delegate.Combine(_OnTemperatureChange, value); 9
10 } 11
12 remove 13 { 14 //此处代码可以自定义
15 Delegate.Remove(_OnTemperatureChange, value); 16 } 17
18 }