一 ObServer模式:
定义对象间的一对多关系,当监视对象(Subject)发生改变后,依赖他的对象会自动告知和更新。
public partial class Heater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HeaterBoil heater = new HeaterBoil();
Alarm alram = new Alarm();
Display display = new Display();
heater.BoilEvent+=new HeaterBoil.BoilHandler(alram.MakeAlert);
heater.BoilEvent += new HeaterBoil.BoilHandler(display.ShowMsg);
heater.BoilWater();
}
//热水器
public class HeaterBoil
{
private int temperature;//水温
public delegate void BoilHandler(int param);//定义委托
public event BoilHandler BoilEvent;//定义事件
//烧水
public void BoilWater()
{
for (int i = 0; i <= 100; i++)
{
temperature = i;
if (temperature > 95)
{
if (BoilEvent != null)
{
BoilEvent(temperature);
}
}
}
}
}
//报警器
public class Alarm:System.Web.UI.Page
{
//发出语音警报
public void MakeAlert(int param)
{
System.Web.HttpContext.Current.Response.Write(string.Format("Alarm:嘀嘀嘀,水已经{0}度了<br>", param));
}
}
// 显示器
public class Display : System.Web.UI.Page
{
//显示水温
public void ShowMsg(int param)
{
System.Web.HttpContext.Current.Response.Write(string.Format("Display:谁快开了,当前温度{0}度。<br>", param));
}
}
}
二 Event事件代码
public partial class EventTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LowCharge+=new LowChargeEventHandler(Test);
LowChargeEventArgs eventTest=new LowChargeEventArgs();
eventTest.Str = "100";
OnLowCharge(eventTest);
}
//定义事件数据类
public class LowChargeEventArgs : EventArgs
{
private string str;
public string Str
{
get { return str; }
set { str = value; }
}
}
//定义事件委托
public delegate void LowChargeEventHandler(object sender, LowChargeEventArgs e);
//定义事件成员
public event LowChargeEventHandler LowCharge;
//调用事件委托
protected virtual void OnLowCharge(LowChargeEventArgs e)
{
if (LowCharge != null)
{
LowCharge(this, e);
}
}
//方法
public void Test(object sender, LowChargeEventArgs e)
{
Response.Write("Test By Event------"+e.Str.ToString());
}
}
三 委托实现代码和几种调用方式实现
public partial class DelegateTest_2 : System.Web.UI.Page
{
//定义委托
public delegate void testDelegate(string para);
protected void Page_Load(object sender, EventArgs e)
{
GetMethod("中国",ResponseA);
GetMethod("俄罗斯", ResponseB);
MethodTest test = new MethodTest();
GetMethod("中国", test.ResponseA);
GetMethod("俄罗斯", test.ResponseB);
testDelegate test2 = new testDelegate(ResponseA);
test2 += ResponseB;
GetMethod("ZZ", test2);
test2 -= ResponseA;
GetMethod("TT", test2);
//使用管理类
GetMethodManager manager = new GetMethodManager();
manager.GetMethod("A",ResponseA);
manager.GetMethod("B", ResponseB);
testDelegate test3 = new testDelegate(ResponseA);
test3 += ResponseB;
manager.GetMethod("T", test3);
manager.MakeDelete+=new testDelegate(ResponseA);
manager.MakeDelete+=new testDelegate(ResponseB);
manager.GetMethod("QQ");
}
protected void GetMethod(string para,testDelegate methodTest)
{
methodTest(para);
}
//封装上面的GetMethod方法为一个类
public class GetMethodManager
{
public event testDelegate MakeDelete;
public void GetMethod(string para, testDelegate methodTest)
{
methodTest(para);
}
public void GetMethod(string para)
{
MakeDelete(para);
}
}
public void ResponseA(string para)
{
Response.Write(para + "---A---");
}
public void ResponseB(string para)
{
Response.Write(para + "---B---");
}
public class MethodTest:System.Web.UI.Page
{
public void ResponseA(string para)
{
System.Web.HttpContext.Current.Response.Write(para + "---A---");
}
public void ResponseB(string para)
{
System.Web.HttpContext.Current.Response.Write(para + "---B---");
}
}
}