在C#.Net中,观察者模式就是对事件委托的完美应用。

在C#.Net中,观察者模式就是对事件委托的完美应用。

C#代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Threading;   
  5.   
  6. namespace Observer   
  7. {   
  8.     /// <summary>   
  9.     /// Observer 观察者模式 入口 .这个才是一个真正的有效的产品。由三个对象构成:烧水品,报警器,显示器   
  10.     /// </summary>   
  11.     class Program   
  12.     {   
  13.         static void Main(string[] args)   
  14.         {   
  15.             Heater heater = new Heater();   
  16.             Alarm alarm = new Alarm();   
  17.   
  18.             heater.BoilEvent += alarm.Alert;         //注册方法   
  19.             heater.BoilEvent += new Alarm().Alert;       给匿名对象注册方法   
  20.             heater.BoilEvent += Display.Show;       //注册静态方法   
  21.   
  22.             heater.BoilWater();      //烧水,会自动调用注册过对象的方法   
  23.   
  24.             Console.ReadLine();   
  25.         }   
  26.     }  
  27.  
  28.  
  29.     #region 热水品成品,由三个部件组成一个有效产品
  30.    //观察的主题,通知所以观察者   
  31.   
  32.     /// <summary>   
  33.     /// 热水器   
  34.     /// </summary>   
  35.     class Heater   
  36.     {   
  37.         private int temperature;      //当前水温   
  38.   
  39.         public delegate void BoilHandler(int t);     //委托   
  40.         public event BoilHandler BoilEvent;      //事件   
  41.   
  42.         public Heater() { }   
  43.   
  44.         /// <summary>   
  45.         /// 烧水的过程   
  46.         /// </summary>   
  47.         public void BoilWater()   
  48.         {   
  49.             for (int i = 0; i <= 100; i++)   
  50.             {   
  51.                 Thread.Sleep(50);      //摸拟烧水耗时,没到设定值时没有动劲   
  52.                 temperature = i;   
  53.                 if (temperature > 92)   
  54.                 {   
  55.                     Thread.Sleep(1500);      //为了显示明显,增加耗时   
  56.                     if (BoilEvent != null)     //如果有对象注册   
  57.                     {   
  58.                         BoilEvent(temperature);      //调用所有注册对象的方法   
  59.                     }   
  60.                 }   
  61.             }   
  62.         }   
  63.     }   
  64.   
  65.   
  66.     /// <summary>   
  67.     /// 报警器   
  68.     /// </summary>   
  69.     class Alarm   
  70.     {   
  71.         public Alarm() { }   
  72.   
  73.         public void Alert(int temperature)   
  74.         {   
  75.             Console.WriteLine("Alarm:嘀嘀嘀,水温已经{0}度了。",temperature);   
  76.         }   
  77.     }   
  78.   
  79.   
  80.     /// <summary>   
  81.     /// 显示器   
  82.     /// </summary>   
  83.     class Display   
  84.     {   
  85.         private Display() { }   
  86.   
  87.         public static void Show(int temperature)   
  88.         {   
  89.             Console.WriteLine("Display:注意,水温{0}度。", temperature);      
  90.         }   
  91.     }  
  92.  
  93.     #endregion    
  94.   
  95.   
  96. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值