您的位置:首页 > 其它

事件处理

2016-05-31 11:36 399 查看
-1-发布者和订阅者的设计模式(Publisher/Subscriber)

    当发布者发布一则讯息的时候,那么他的订阅者就都会收到此讯息,并进行相应的     判断。我们可以通过之前的委托类型来实现此功能。

-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);
}
};
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: