(C#)设计模式 之 观察者模式 (经典应用:猫叫,烧开水)
2012-09-03 18:31
826 查看
转自:/content/4427617.html
/* * 题目: * 猫叫了,所有老鼠开始逃跑,主人被惊醒,请用OO的思想描绘此过程 * 1,老鼠跟主人是被动的 * 2,要考虑联动性与扩展性 */ using System; using System.Collections.Generic; using System.Text; namespace CatCry { /*委托,当猫叫时将信息传给所有观察者*/ public delegate void CatCryEventHandler(object sender, EventArgs e); /*被观察对象*/ abstract class Subject { public event CatCryEventHandler CatCryEvent;//猫叫时的事件 public void Cry(EventArgs e) { this.CatCryEvent(this, e); } public void Cry() { //不接收猫叫时发出的信息 this.CatCryEvent(this, new EventArgs()); } } /*观察者*/ abstract class Observer { public Observer() { } public Observer(Subject subject) { subject.CatCryEvent += new CatCryEventHandler(this.Notified); } //当猫叫时做出的反应(当收到通知时) abstract protected void Notified(object sender, EventArgs e); } /*定义猫作为被观察对象*/ class Cat : Subject { public Cat() { Console.WriteLine("猫叫了"); } } /*定义老鼠作为观察者*/ class Mouse : Observer { string mouseName; public Mouse() { } public Mouse(string mouseName, Subject subject) : base(subject) { this.mouseName = mouseName; } override protected void Notified(object sender, EventArgs e) { Console.WriteLine(this.mouseName + "开始逃跑"); } } /*定义主人作为观察者*/ class Marster : Observer { public Marster() { } public Marster(Subject subject) : base(subject) { } override protected void Notified(object sender, EventArgs e) { Console.WriteLine("主人被惊醒了"); } } /// <summary> /// 功能:用观察者模式实现 /// 作者:allnen /// 时间:2008-6-5 /// </summary> class Program { static void Main(string[] args) { Cat cat = new Cat(); Mouse m1 = new Mouse("老鼠1", cat); Mouse m2 = new Mouse("老鼠2", cat); Marster marster = new Marster(cat); cat.Cry();//猫叫了 } } }
/* * code 烧开水 */ using System; using System.Collections.Generic; using System.Text; namespace Delegate { // 热水器 public class Heater { private int temperature; public string type = "RealFire 001"; // 添加型号作为演示 public string area = "China Xian"; // 添加产地作为演示 //声明委托 public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e); public event BoiledEventHandler Boiled; //声明事件 // 定义BoiledEventArgs类,传递给Observer所感兴趣的信息 public class BoiledEventArgs : EventArgs { public readonly int temperature; public BoiledEventArgs(int temperature) { this.temperature = temperature; } } // 可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视 protected virtual void OnBoiled(BoiledEventArgs e) { if (Boiled != null) { // 如果有对象注册 Boiled(this, e); // 调用所有注册对象的方法 } } // 烧水。 public void BoilWater() { for (int i = 0; i <= 100; i++) { temperature = i; if (temperature > 95) { //建立BoiledEventArgs 对象。 BoiledEventArgs e = new BoiledEventArgs(temperature); OnBoiled(e); // 调用 OnBolied方法 } } } } // 警报器 public class Alarm { public void MakeAlert(Object sender, Heater.BoiledEventArgs e) { Heater heater = (Heater)sender; //这里是不是很熟悉呢? //访问 sender 中的公共字段 Console.WriteLine("Alarm:{0} - {1}: ", heater.area, heater.type); Console.WriteLine("Alarm: 嘀嘀嘀,水已经 {0} 度了:", e.temperature); Console.WriteLine(); } } // 显示器 public class Display { public static void ShowMsg(Object sender, Heater.BoiledEventArgs e) { //静态方法 Heater heater = (Heater)sender; Console.WriteLine("Display:{0} - {1}: ", heater.area, heater.type); Console.WriteLine("Display:水快烧开了,当前温度:{0}度。", e.temperature); Console.WriteLine(); } } class Program { static void Main() { Heater heater = new Heater(); Alarm alarm = new Alarm(); heater.Boiled += alarm.MakeAlert; //注册方法 heater.Boiled += (new Alarm()).MakeAlert; //给匿名对象注册方法 heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert); //也可以这么注册 heater.Boiled += Display.ShowMsg; //注册静态方法 heater.BoilWater(); //烧水,会自动调用注册过对象的方法 } } } //输出为: //Alarm:China Xian - RealFire 001: //Alarm: 嘀嘀嘀,水已经 96 度了: //Alarm:China Xian - RealFire 001: //Alarm: 嘀嘀嘀,水已经 96 度了: //Alarm:China Xian - RealFire 001: //Alarm: 嘀嘀嘀,水已经 96 度了: //Display:China Xian - RealFire 001: //Display:水快烧开了,当前温度:96度。
相关文章推荐
- 观察者模式的经典应用(猫叫 烧开水)
- C# 通过 Observer观察者 设计模式 来理解 抽象类 和 接口 应用在什么地方
- 面试习题之设计模式 C#观察者模式(猫叫老鼠惊走主人醒)
- c#设计模式-观察者模式
- C#中利用代理实现观察者设计模式
- iOS应用开发中使用设计模式中的观察者模式的实例
- C#设计模式-观察者模式(Observer Pattern)[FreeDownload]
- C#中23个经典设计模式(1)——ABSTRACT FACTORY(抽象工厂)
- C#设计模式——观察者模式(Observer Pattern)
- C#中利用代理实现观察者设计模式详解
- c#设计模式-观察者模式
- 用事件和观察者模式解决经典面向对象题目:猫叫鼠跑人醒的题目
- 设计模式-观察者模式应用
- c# 设计模式:观察者模式
- 【C#】Event事件的应用之观察者模式
- [导入]C#面向对象设计模式纵横谈(19):(行为型模式) Observer 观察者模式.zip(10.27 MB)
- 乐在其中设计模式(C#) - 观察者模式(Observer Pattern)
- C#设计模式系列:观察者模式(Observer)
- State模式的经典应用场景:订单处理(c#实现)
- 设计模式应用与发展之观察者模式(java)