日期:2014-05-17 浏览次数:20884 次
//烧水器(被观察者)
public class Heater
{
private int temperature;
private string Place = "上海宝山";
private string Time = "2013年11月7号";
public delegate void BoiledEventHandler(object sender,BoiledEventArgs e);
public event BoiledEventHandler Boiled;
//定义BoiledEventArgs类,传递给Observer所感兴趣的信息
public class BoiledEventArgs : EventArgs
{
public readonly int temperature;
public readonly string Place;
public readonly string Time;
//构造函数
public BoiledEventArgs(int temperature,string Place,string Time)
{
this.temperature = temperature;
this.Place = Place;
this.Time = Time;
}
}
//可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视
protected virtual void OnBoiled(BoiledEventArgs e)
{
if (Boiled != null)
{ // 如果有对象注册
Boiled(this, e); // 调用所有注册对象的方法
}
}
//烧水
public void BoilWater()
{
for (int i = 0; i <= 100; i++)
{
temperature = i;
if (temperature > 95)
{
&nb