C#的事件、委托测试(一) http://47.98.154.65/?id=1853
C#的事件、委托测试(二) http://47.98.154.65/?id=1854
C#的事件、委托测试(三) http://47.98.154.65/?id=1820
勇哥注:
事件委托的小练习,大家不要轻视这些基本功哦。
第一个程序:
目的:
我们有个高档的热水器,我们给它通上电,当水温超过95度的时候:1、扬声器会开始发出语音,告诉你水的温度;2、液晶屏也会改变水温的显示,来提示水已经快烧开了
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Heater ht = new Heater(); ht.BoilWater(); } } class Heater { private int temperature; // 水温 // 烧水 public void BoilWater() { for (int i = 0; i <= 100; i++) { temperature = i; if (temperature > 95) { MakeAlert(temperature); ShowMsg(temperature); } } } // 发出语音警报 private void MakeAlert(int param) { Console.WriteLine($"Alarm:嘀嘀嘀,水已经 {param}度了"); } // 显示水温 private void ShowMsg(int param) { Console.WriteLine($"Display:水快开了,当前温度:{param}"); } } }
第二个程序
目的:
假设热水器由三部分组成:热水器、警报器、显示器,它们来自于不同厂商并进行了组装。那么,应该是热水器仅仅负责烧水,它不能发出警报也不能显示水温;在水烧开时由警报器发出警报、显示器显示提示和水温
因此,需要划分出三个对象类。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { } } class Heater { private int temperature; // 水温 // 烧水 public void BoilWater() { for (int i = 0; i <= 100; i++) { temperature = i; } } } public class CAlert { // 发出语音警报 private void MakeAlert(int param) { Console.WriteLine($"Alarm:嘀嘀嘀,水已经 {param}度了"); } } public class CDisplay { // 显示水温 private void ShowMsg(int param) { Console.WriteLine($"Display:水快开了,当前温度:{param}"); } } }
第三个程序
目的:
如何在水烧开的时候通知报警器和显示器?
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var c1 = new CAlert(); var c2 = new CDisplay(); var h = new Heater(); h.BoilEvent += c1.MakeAlert; h.BoilEvent += c2.ShowMsg; h.BoilWater(); } } class Heater { private int temperature; // 水温 public delegate void BoilHandle(int param); public event BoilHandle BoilEvent; // 烧水 public void BoilWater() { for (int i = 0; i <= 100; i++) { temperature = i; if(temperature>95) { if (null != BoilEvent) BoilEvent(temperature); } } } } public class CAlert { // 发出语音警报 public void MakeAlert(int param) { Console.WriteLine($"Alarm:嘀嘀嘀,水已经 {param}度了"); } } public class CDisplay { // 显示水温 public void ShowMsg(int param) { Console.WriteLine($"Display:水快开了,当前温度:{param}"); } } }
实际上,上面的代码完成的就是观察者模式。
Observer设计模式,Observer设计模式中主要包括如下两类对象:
Subject:监视对象,它往往包含着其他对象所感兴趣的内容。在本范例中,热水器就是一个监视对象,它包含的其他对象所感兴趣的内容,就是temprature字段,当这个字段的值快到100时,会不断把数据发给监视它的对象。
Observer:监视者,它监视Subject,当Subject中的某件事发生的时候,会告知Observer,而Observer则会采取相应的行动。在本范例中,Observer有警报器和显示器,它们采取的行动分别是发出警报和显示水温。
在本例中,事情发生的顺序应该是这样的:
警报器和显示器告诉热水器,它对它的温度比较感兴趣(注册)。
热水器知道后保留对警报器和显示器的引用。
热水器进行烧水这一动作,当水温超过95度时,通过对警报器和显示器的引用,自动调用警报器的MakeAlert()方法、显示器的ShowMsg()方法。
类似这样的例子是很多的,GOF对它进行了抽象,称为Observer设计模式:Observer设计模式是为了定义对象间的一种一对多的依赖关系,以便于当一个对象的状态改变时,其他依赖于它的对象会被自动告知并更新。Observer模式是一种松耦合的设计模式。
第四个程序
目的:
把程序三改为符合 .Net Framework中的委托与事件规范。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp6 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var c1 = new CAlert(); var c2 = new CDisplay(); var h = new Heater(); h.Boiled += c1.MakeAlert; h.Boiled += c2.ShowMsg; h.BoilWater(); } } //注意应该为: public class Heater //否则有访问级别低的问题。 class Heater { private int temperature; // 水温 public string type = "RealFire001"; // 添加额外属性:型号作为演示 public string area = "WuHan"; // 添加产地作为演示 public delegate void BoilEventHandle(object sender, BoilEventArgs e); public event BoilEventHandle Boiled; public class BoilEventArgs:EventArgs { public readonly int temperature; public BoilEventArgs(int t) { this.temperature = t; } } protected virtual void OnBoiled(BoilEventArgs e) { if (null != e) Boiled(this, e); } // 烧水 public void BoilWater() { for (int i = 0; i <= 100; i++) { temperature = i; if (temperature > 95) { var e = new BoilEventArgs(temperature); OnBoiled(e); } } } } public class CAlert { // 发出语音警报 public void MakeAlert(object sender, Heater.BoilEventArgs e) { var h = sender as Heater; Console.WriteLine($"Alarm:{h.area},{h.type} 嘀嘀嘀,水已经 {e.temperature}度了"); } } public class CDisplay { // 显示水温 public void ShowMsg(object sender, Heater.BoilEventArgs e) { var h = sender as Heater; Console.WriteLine($"Display:{h.area},{h.type} 水快开了,当前温度:{e.temperature}"); } } }
这个代码还存在一个编译错误:
我还以为内部类public class BoilEventArgs 是不是有什么访问限制。明明是public还是不行。
其实我是想多了,原因是class Heater忘记标记为public
不过即使class Heater是private,我们还是可以public class BoilEventArgs这个类放到命名空间级别去定义,还是可以的。
最后是关于.net 事件规范的说明:
.Net Framework中的委托与事件 .Net Framework的编码规范: 委托类型的名称都应该以EventHandler结束。 委托的原型定义:有一个void返回值,并接受两个输入参数:一个Object 类型,一个 EventArgs类型(或继承自EventArgs)。 事件的命名为 委托去掉 EventHandler之后剩余的部分。 继承自EventArgs的类型应该以EventArgs结尾。 再做一下说明: 委托声明原型中的Object类型的参数代表了Subject,也就是监视对象,在本例中是 Heater(热水器)。 回调函数(比如Alarm的MakeAlert)可以通过它访问触发事件的对象(Heater)。 EventArgs 对象包含了Observer所感兴趣的数据,在本例中是temperature。 上面这些其实不仅仅是为了编码规范而已,这样也使得程序有更大的灵活性。 比如说,如果我们不光想获得热水器的温度,还想在Observer端(警报器或者显示器)方法中获得它的生产日期、型号、价格, 那么委托和方法的声明都会变得很麻烦,而如果我们将热水器的引用传给警报器的方法,就可以在方法中直接访问热水器了。

