当前位置: 首页 > news >正文

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

相关文章:

  • Mysql中文乱码的解决方法
  • 关于功能的设计
  • 清楚数据库中的全部数据并将ID归零
  • Java Web项目开发到底需要掌握哪些技术?
  • C# 判断时间段是否相交
  • lyo blog3D文章集锦
  • 使用c# Mongo Driver 完成嵌套查询
  • 过渡到SSAS之一:简单模型认识
  • 谈谈团队合作中的设计与优化
  • 页面元素的定位scrollintoview
  • C#将子线程附加在当前线程之后执行
  • 过渡到SSAS之二:服务的发布
  • MongoDb 中的PageFilter
  • 新浪换标,也许应该做点什么了
  • 使用joint.js 绘制图
  • .pyc 想到的一些问题
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • C++11: atomic 头文件
  • DataBase in Android
  • es6
  • HTML中设置input等文本框为不可操作
  • Javascripit类型转换比较那点事儿,双等号(==)
  • k个最大的数及变种小结
  • mac修复ab及siege安装
  • mongo索引构建
  • Nacos系列:Nacos的Java SDK使用
  • PAT A1017 优先队列
  • spark本地环境的搭建到运行第一个spark程序
  • 程序员该如何有效的找工作?
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 多线程 start 和 run 方法到底有什么区别?
  • 爬虫进阶 -- 神级程序员:让你的爬虫就像人类的用户行为!
  • 前端工程化(Gulp、Webpack)-webpack
  • 前端性能优化--懒加载和预加载
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 通过调用文摘列表API获取文摘
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • #define与typedef区别
  • #NOIP 2014# day.2 T2 寻找道路
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (4)Elastix图像配准:3D图像
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (补)B+树一些思想
  • (二)WCF的Binding模型
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (十三)Maven插件解析运行机制
  • (十一)JAVA springboot ssm b2b2c多用户商城系统源码:服务网关Zuul高级篇
  • (原創) 系統分析和系統設計有什麼差別? (OO)
  • .NET Framework 4.6.2改进了WPF和安全性
  • .NET gRPC 和RESTful简单对比
  • .NET Micro Framework初体验(二)