c#观察者模式和事件委托的联合使用
来源:互联网 发布:360软件管理下载 编辑:程序博客网 时间:2024/05/17 04:17
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;//观察者模式和事件委托的联合使用namespace 委托和事件2{ //热水器的类,被监视的对象 public class Heater { private int temperature;//水温 public delegate void heatEventHandler(int para); public event heatEventHandler heat; //声明事件 事件的命名为委托去掉EventHandler之后的部分 //烧水 public void boilWater() { for (int i = 0; i <= 100; i++) { temperature = i; if (temperature >= 98) { if (heat != null) //有方法注册进事件中 { heat(temperature); //在条件满足的情况下通知观察者 } } } } } //警报类,observer的角色 public class Alarm { public void boilVoice(int para) { Console.WriteLine("嘀嘀嘀"); } } //显示类。显示水温,observer的角色 public class Show { public void ShowTemperature(int para) { Console.WriteLine("水温已经{0}度了",para); } } // class Program { static void Main(string[] args) { Heater heater = new Heater(); Alarm alarm = new Alarm(); Show show = new Show(); heater.heat += alarm.boilVoice; heater.heat += show.ShowTemperature; heater.boilWater(); Console.Read(); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 事件访问器{ //定义委托 public delegate string GeneralEventHandler(); //定义事件的发布者 public class Publisher { private GeneralEventHandler _delegate; public event GeneralEventHandler geneal //事件访问器的定义 { add { _delegate = value; } remove { _delegate -= value; } } public void DoSomething() { if (_delegate != null) { string str = _delegate(); Console.WriteLine(str); } } } //定义事件的订阅者 public class Subscriber { public string method() { return "liK"; } } class Program { static void Main(string[] args) { Publisher ps = new Publisher(); Subscriber sb = new Subscriber(); ps.geneal += sb.method; ps.DoSomething(); Console.Read(); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 委托和事件返回值研究{ public delegate string testEventHandler(); class AA { public testEventHandler test; public void method() { if (test != null) { Console.WriteLine(test()); } } } class BB { public static string bb() { return "likang01"; } } class CC { public static string cc() { return "likang02"; } } class Program { static void Main(string[] args) { AA aa = new AA(); //委托变量可以供对个订阅者注册,如果定义了返回值,那么对个订阅者都会向发布者返回数值 aa.test = BB.bb; //结果就是后面的一个返回值将前面的返回值覆盖了,实际上只能获得最后一个方法调用的返回值 aa.test += CC.cc; aa.test -= CC.cc; aa.method(); Console.Read(); } }}
这篇文章是学习博客园一位大神的笔记吧,大神还写了事件和委托的高级篇,无赖不甚理解,暂时不上了,要看的直接访问http://www.cnblogs.com/JimmyZhang/archive/2008/08/22/1274342.html
阅读全文
0 0
- c#观察者模式和事件委托的联合使用
- 观察者设计模式(C#委托和事件的使用)
- C# 观察者模式和委托、事件
- C#委托和事件 观察者模式理解
- c#委托和事件及观察者模式
- C# 中的委托和事件 观察者模式
- C#观察者模式-事件委托
- C# 观察者模式(委托的使用)
- 用C#中的委托和事件简单实现观察者模式
- C# 中的委托和事件 以及观察者模式
- C#中使用委托和事件实现观察者模式(observer pattern)
- C#中使用委托和事件实现观察者模式(observer pattern)
- 在C#中使用代理(即委托)的方式触发事件(观察者模式)
- c# 委托 事件 EventHandler 观察者模式 详解,以及和java的比较
- C#中委托,事件以及观察者模式
- C# ~ 从 委托事件 到 观察者模式
- C# 委托 事件 观察者模式 总结
- C#委托 和代理模式 观察者模式的关系
- 关于面试
- Spring源码解析——如何阅读源码
- React之智能组件和木偶组件
- 金额计算
- 715. Range Module
- c#观察者模式和事件委托的联合使用
- Android多线程--HandlerThread的使用以及原理
- asp.net 程序 Respons.redirect("XXXXXX")无法跳转
- 解决python-opencv中路径不能读中文的问题
- android handler Looper MessageQueue源码分析
- CCF 201703-1 分蛋糕
- PostgreSQL java连接SQL(七)
- 获取IP地址
- 编译contrib时遇到的问题