事件处理
2016-05-31 11:36
399 查看
-1-发布者和订阅者的设计模式(Publisher/Subscriber)
当发布者发布一则讯息的时候,那么他的订阅者就都会收到此讯息,并进行相应的 判断。我们可以通过之前的委托类型来实现此功能。
-2-订阅者只需要将自己的委托处理方法加入到委托类型中即可。如果取消订阅,只需要从其中去除即可,比较简单,可以在定义委托对象时加上event,目的是为了让当前的订阅者不会影响到其他的订阅者。
下面是一段红绿灯为发布者,汽车为订阅者的模拟代码:
-3-使用EventHandler类
在事件发布和订阅的过程中,定义事件的原型委托类型是一件重复性的工作,过 于麻烦,为此.NET类库中定义了一个EventHandler委托类型:
public delegate void EventHandler (object sender,EventArgs e);
其中sender顾名思义,引发事件的对象。
e表示出了sender里的数据之外,还要发送给订阅者的讯息。
使用的时候需要对其进行拆箱转换TrafficLight light = (TrafficLight)sender;
下面的程序还有了在事件中使用匿名的方法和EventHandler类型。
当发布者发布一则讯息的时候,那么他的订阅者就都会收到此讯息,并进行相应的 判断。我们可以通过之前的委托类型来实现此功能。
-2-订阅者只需要将自己的委托处理方法加入到委托类型中即可。如果取消订阅,只需要从其中去除即可,比较简单,可以在定义委托对象时加上event,目的是为了让当前的订阅者不会影响到其他的订阅者。
下面是一段红绿灯为发布者,汽车为订阅者的模拟代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 委托的发布和订阅 { class Program { static void Main(string[] args) { TrafficLight light = new TrafficLight(); Car car1 = new Car(); Ambulance car2 = new Ambulance(); car1.Enter(light); car2.Enter(light); light.changecolor(); light.changecolor(); car2.Emergent = true; light.changecolor(); car1.Leave(light); light.changecolor(); } } public delegate void LightEvent(bool color); public class TrafficLight { private bool color=false; public bool Color { get { return color; } } public event LightEvent OnColorChange; public void changecolor() { color=!color; Console.WriteLine(color ? "红灯亮": "绿灯亮"); if(OnColorChange !=null) { OnColorChange(color); } } } public class Car { private bool isRun=true; public void Enter(TrafficLight light) { light.OnColorChange += car_color_change; } public void Leave(TrafficLight light) { light.OnColorChange -= car_color_change; } public virtual void car_color_change(bool color) { if(color&&isRun ) { isRun = false; Console.WriteLine("{0}停车", this); } else if(!color&&!isRun) { isRun = true; Console.WriteLine("{0}启动", this); } } } public class Ambulance : Car { private bool emergent = false; public bool Emergent { get { return emergent; } set { emergent=value ; } } public override void car_color_change(bool color) { if (emergent) Console.WriteLine("{0}紧急通行", this); else base.car_color_change(color); } } }
-3-使用EventHandler类
在事件发布和订阅的过程中,定义事件的原型委托类型是一件重复性的工作,过 于麻烦,为此.NET类库中定义了一个EventHandler委托类型:
public delegate void EventHandler (object sender,EventArgs e);
其中sender顾名思义,引发事件的对象。
e表示出了sender里的数据之外,还要发送给订阅者的讯息。
使用的时候需要对其进行拆箱转换TrafficLight light = (TrafficLight)sender;
下面的程序还有了在事件中使用匿名的方法和EventHandler类型。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 委托的发布和订阅 { class Program { public delegate void EventHandler(object sender, EventArgs e); static void Main(string[] args) { TrafficLight light = new TrafficLight(); Car car1 = new Car(); //Car car2 = new Ambulance(); car1.Enter(light); light.changecolor(60); light.changecolor(30); } public class Lighteventargs:EventArgs { private int seconds; public int Seconds { get { return seconds; } } public Lighteventargs (int seconds) { this.seconds = seconds; } } public class TrafficLight { private bool color=false; public bool Color { get { return color; } } public event EventHandler OnColorChange; public void changecolor(int seconds) { color=!color; Console.WriteLine(color ? "红灯亮": "绿灯亮"); if(OnColorChange !=null) { OnColorChange(this, new Lighteventargs(seconds)); } } } public class Car { private bool isRun=true; public virtual void Enter(TrafficLight light) { light.OnColorChange += delegate (object sender, EventArgs e)//匿名方法 { if (light.Color) { isRun = false; Console.WriteLine("{0}停车,{1}秒后启动", this, ((Lighteventargs )e).Seconds); } else { isRun = true; Console.WriteLine("{0}启动,{1}秒后通过", this, ((Lighteventargs )e).Seconds); } }; } } } }
相关文章推荐
- iOS开发 UICollectionView属性介绍
- tm结构
- 特殊IP地址(段)
- HVR日志分析简单应用
- 多维数组与交错数组的转换
- 遍历二叉树
- [置顶] 算法——选择、冒泡、插入
- Docker 常用命令
- Android 源码编译过程解释
- linux命令
- [Error] ISO C++ forbids comparison between pointer and integer [-fpermiss
- JAVA实验六文件的输入输出的应用
- 设置Eclipse界面颜色
- UICollectionView基础
- c# 打印的问题总结
- java反射
- linux 下git使用教程
- 动态规划之最大子段和问题
- 数字哑谜
- GitHub 发布了 Electron 1.0 版本,帮助开发者打造更强大的原生应用