zoukankan      html  css  js  c++  java
  • 观察者模式与.Net Framework中的委托与事件

       本文文字内容均选自《大话设计模式》一书。

        解释:观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。

        此模式又叫发布-订阅模式。

        举例:火车到站与乘客下车。

        主题:

     1 using System;
     2 using System.Threading;
     3 
     4 namespace DelegateDemo2
     5 {
     6     public class 高速列车
     7     {
     8         public string 车次 { get; set; }
     9         private string[] 途经车站;
    10         private string 当前到站;
    11 
    12         public delegate void 到站EventHandler(Object sender, 到站EventArgs e);
    13         public event 到站EventHandler 到站;
    14 
    15         public class 到站EventArgs : EventArgs
    16         {
    17             public readonly string 当前到站;
    18             public 到站EventArgs(string 当前到站)
    19             {
    20                 this.当前到站 = 当前到站;
    21             }
    22         }
    23 
    24         public 高速列车()
    25         {
    26             this.车次 = "G253";
    27             this.途经车站 = new string[] { "青岛站", "济南站", "泰安站", "徐州站", "南京站", "苏州站", "杭州站" };
    28         }
    29 
    30         protected void On到站(到站EventArgs e)
    31         {
    32             if (this.到站 != null)
    33             {
    34                 this.到站(this, e);
    35             }
    36         }
    37 
    38         public void 行驶()
    39         {
    40             for (int i = 0; i < this.途经车站.Length; i++)
    41             {
    42                 this.当前到站 = this.途经车站[i];
    43                 到站EventArgs e = new 到站EventArgs(this.当前到站);
    44                 On到站(e);
    45 
    46                 Thread.Sleep(1000 * 3);
    47             }
    48         }
    49     }
    50 }

        观察者1:

     1 using System;
     2 
     3 namespace DelegateDemo2
     4 {
     5     public class 显示器
     6     {
     7         public void 显示到站信息(Object sender, DelegateDemo2.高速列车.到站EventArgs e)
     8         {
     9             高速列车 高速列车 = (高速列车)sender;
    10             Console.WriteLine("{0}次列车当前已到达{1}。", 高速列车.车次, e.当前到站);
    11         }
    12     }
    13 }

        观察者2:

    using System;
    
    namespace DelegateDemo2
    {
        public class 乘客
        {
            public string 姓名 { get; set; }
            public string 目的地 { get; set; }
    
            public 乘客(string 姓名, string 目的地)
            {
                this.姓名 = 姓名;
                this.目的地 = 目的地;
            }
    
            public void 提行李下车(Object sender, DelegateDemo2.高速列车.到站EventArgs e)
            {
                if (e.当前到站 == this.目的地)
                {
                    Console.WriteLine("乘客({0})已到达目的地{1},提行李下车!", this.姓名, e.当前到站);
                }
            }
        }
    }

        客户端:

     1 namespace DelegateDemo2
     2 {
     3     class Program
     4     {
     5         static void Main(string[] args)
     6         {
     7             高速列车 高速列车 = new 高速列车();
     8             显示器 显示器 = new 显示器();
     9 
    10             乘客 张三丰 = new 乘客("张三丰", "济南站");
    11             乘客 风清扬 = new 乘客("风清扬", "南京站");
    12             乘客 扫地僧 = new 乘客("扫地僧", "杭州站");
    13 
    14             高速列车.到站 += new 高速列车.到站EventHandler(显示器.显示到站信息);
    15             高速列车.到站 += new DelegateDemo2.高速列车.到站EventHandler(张三丰.提行李下车);
    16             高速列车.到站 += new DelegateDemo2.高速列车.到站EventHandler(风清扬.提行李下车);
    17             高速列车.到站 += new DelegateDemo2.高速列车.到站EventHandler(扫地僧.提行李下车);
    18 
    19             高速列车.行驶();
    20         }
    21     }
    22 }

        使用情景:当一个对象的改变需要同时改变其他对象,且不知道具体有多少对象有待改变时,应该考虑使用观察者模式。

        【委托】:委托可以看作是对函数的抽象,是函数的“类”,委托的实例将代表一个具体的函数。

        一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值。

        而且,一个委托可以搭载多个方法,所有方法被依次唤起。更重要的是,它可以使得委托对象所搭载的方法并不需要属于同一个类。

  • 相关阅读:
    Math.floor,Math.ceil,Math.rint,Math.round用法
    double类型转换为int类型四舍五入工具类
    精度更高的double类型计算工具类(借助BigDecimal类型)
    Java中float和double转换的问题
    Java对数
    iPhone内存溢出——黑白苹果
    VBS猜数游戏
    HTML_1
    MySQL学习
    Python进制转换
  • 原文地址:https://www.cnblogs.com/MT-talent/p/10051690.html
Copyright © 2011-2022 走看看