C# 利用事件实现观察者模式
2011-04-25 22:08
716 查看
摘要本程序场景上下文如下:
有一个学校,由于这个学校的学风不太好,同学们和校警听到第三次铃声后,才分别走进教室和关闭学校大门。
代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication
7 {
8 /*
9 * 本场景说明如下:
* 由于这个学校的学风不太好,同学们和校警听到第三次铃声后,才去做他们应该做的事。
**/
class Program
{
static void Main(string[] args)
{
var stu = new Student();
var plm = new Policeman();
var bel = new Bell();
bel.Name = "A-289";
bel.Ringing += new Bell.RingHandler(stu.OnTingRing);
bel.Ringing += new Bell.RingHandler(plm.OnTRing);
bel.OnRinging(new RingEventArgs(3));
}
}
//观察者
class Student
{
public void OnTingRing(object sender, RingEventArgs e)
{
Console.WriteLine(string.Format("when student hearing the {0} ringing for {1} times,go to classroom", ((Bell)sender).Name, e.times));
}
}
//观察者
class Policeman
{
public void OnTRing(object sender, RingEventArgs e)
{
Console.WriteLine(string.Format("when school policeman hearing the {0} ringing for {1} times,close the school's gate", ((Bell)sender).Name, e.times));
}
}
//目标对象
class Bell
{
public delegate void RingHandler(object sender, RingEventArgs e);
public event RingHandler Ringing;
public string Name { get; set; }
public void OnRinging(RingEventArgs e)
{
if (Ringing != null)
{
Ringing(this, e);
}
}
}
class RingEventArgs : EventArgs
{
public RingEventArgs(int times)
{
this.times = times;
}
//次数
public int times { get; set; }
} }
输出结果如下:
when student hearing the A-289 ringing for 3 times,go to classroomwhen school policeman hearing the A-289 ringing for 3 times,close the school's gate
有一个学校,由于这个学校的学风不太好,同学们和校警听到第三次铃声后,才分别走进教室和关闭学校大门。
代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication
7 {
8 /*
9 * 本场景说明如下:
* 由于这个学校的学风不太好,同学们和校警听到第三次铃声后,才去做他们应该做的事。
**/
class Program
{
static void Main(string[] args)
{
var stu = new Student();
var plm = new Policeman();
var bel = new Bell();
bel.Name = "A-289";
bel.Ringing += new Bell.RingHandler(stu.OnTingRing);
bel.Ringing += new Bell.RingHandler(plm.OnTRing);
bel.OnRinging(new RingEventArgs(3));
}
}
//观察者
class Student
{
public void OnTingRing(object sender, RingEventArgs e)
{
Console.WriteLine(string.Format("when student hearing the {0} ringing for {1} times,go to classroom", ((Bell)sender).Name, e.times));
}
}
//观察者
class Policeman
{
public void OnTRing(object sender, RingEventArgs e)
{
Console.WriteLine(string.Format("when school policeman hearing the {0} ringing for {1} times,close the school's gate", ((Bell)sender).Name, e.times));
}
}
//目标对象
class Bell
{
public delegate void RingHandler(object sender, RingEventArgs e);
public event RingHandler Ringing;
public string Name { get; set; }
public void OnRinging(RingEventArgs e)
{
if (Ringing != null)
{
Ringing(this, e);
}
}
}
class RingEventArgs : EventArgs
{
public RingEventArgs(int times)
{
this.times = times;
}
//次数
public int times { get; set; }
} }
输出结果如下:
when student hearing the A-289 ringing for 3 times,go to classroomwhen school policeman hearing the A-289 ringing for 3 times,close the school's gate
相关文章推荐
- Delphi的基于接口(IInterface)的多播监听器模式(观察者模式 ),利用RTTI实现Delphi的多播事件代理研究
- C#中使用委托和事件实现观察者模式(observer pattern)
- C#中利用代理实现观察者设计模式详解
- 利用接口实现自定义监听事件以及观察者模式
- 用C#中的委托和事件简单实现观察者模式
- C#中使用委托和事件实现观察者模式(observer pattern)
- C#中利用代理实现观察者设计模式
- ExtJS要利用观察者模式 去实现自定义的事件
- C#中利用代理实现观察者设计模式
- 利用Observable、Observer实现观察者模式
- [JavaScrip和C#两种语言]实现观察者模式
- 【设计模式】观察者模式(observer)c#/python实现
- C#观察者模式的实现的一点理解。
- unity3d学习笔记(十八)--利用C#的代理和事件结合NGUI的按钮实现施放
- Observer Pattern(观察者模式)及其在C#中的实现
- 利用观察者模式实现通知
- 《Effective C#》读书笔记——条目25:用事件模式实现通知<使用C#表达设计>
- Unity之C#——委托与事件,观察者模式,猫和老鼠事例
- 利用委托实现观察者模式