`
izuoyan
  • 浏览: 8951946 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

C#委托和事件,ObServer模式实例代码

阅读更多

一 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---");
}
}
}

分享到:
评论

相关推荐

    详解C#委托,事件,Observer设计模式

    详解C#委托,事件,Observer设计模式 1.将方法作为方法的参数 2.将方法绑定到委托 3.事件的由来 4.事件和委托的编译代码 5.委托、事件与Observer设计模式 6..Net Framework中的委托与事件

    C#中委托与事件的区别

    委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论

    C#中的委托、事件和Observer设计模式使用方法示例

    此文档有助于学习C#委托、事件和Observer设计模式。

    C#的委托和事件详解

    委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    委托、事件与Observer设计模式

    以实例方式详细讲述了委托、事件与Observer设计模式,每一步都有注释说明,便于大家理解。 适合新手入门理解什么是委托、事件与Observer设计模式

    C#中的委托和事件详解(含源码)

    委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    C#委托与事件

    委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    C#中委托和事件 方法的使用

    C# 中的委托和事件 将方法作为方法的参数 将方法绑定到委托 ...事件和委托的编译代码 委托、事件与Observer设计模式 Observer设计模式简介 实现范例的Observer设计模式 .Net Framework中的委托与事件

    C#委托与事件的经典讲解

    委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    .net C#中的委托和事件

    委托和事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    认识C#中的委托和事件

    菜鸟进入,认识C#中的委托和...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    C#中的委托和事件(HTML)

    委托 和 事件在 .Net Framework中的应用...本文中,将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    C#中的委托和事件

    委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    C# 中的委托和事件

    委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    C#委托和事件深入研究

    委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解...本文将通过范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义...

    c#的委托和事件教程

    委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    C#中的委托和事件(完整版)

    委托和事件在.NET Framework 中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不 长的人来说并不容易。它们就像是一道槛儿,过...托和事件对Observer 设计模式的意义,对它们的编译代码也做了讨论。 1.1

    C#中的委托和事件chm

    委托 和 事件在 .Net Framework中的...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

    C# 中的委托和事件.doc

    引言 委托 和 事件在 .Net ...本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论。

Global site tag (gtag.js) - Google Analytics