zoukankan      html  css  js  c++  java
  • C#事件委托 和 观察者模式之比较

    namespace MyCollections 
    {
       
    using System.Collections;
       
    //-----------------------------------------------------------------------------
       
    //该委托定义相当于观察者模式中的 Notify()函数,
       
    //用来通知观察者关于subject 的变化
       
    //通知观察者,观察者会自动执行观察者内的Update(),Update()方法会暴露给subjects.
       
    // A delegate type for hooking up change notifications.
       public delegate void ChangedEventHandler(object sender, EventArgs e);
       
       
    //------------------------------------------------------------------------------
       
    //该类相当于观察者模式中的subjects.

       
    // A class that works just like ArrayList, but sends event
       
    // notifications whenever the list changes.
       public class ListWithChangedEvent: ArrayList 
       
    {
          
    // An event that clients can use to be notified whenever the
          
    // elements of the list change.
          public event ChangedEventHandler Changed;     //事件相当于实做Notify()函数

          
    // Invoke the Changed event; called whenever list changes
          protected virtual void OnChanged(EventArgs e) 
          
    {
             
    if (Changed != null)                       //这里表示subjects状态有变化,需要通知observers.
                Changed(this, e);
          }


          
    // Override some of the methods that can change the list;
          
    // invoke event after each
          public override int Add(object value) 
          
    {
             
    int i = base.Add(value);
             OnChanged(EventArgs.Empty);
             
    return i;
          }


          
    public override void Clear() 
          
    {
             
    base.Clear();
             OnChanged(EventArgs.Empty);
          }


          
    public override object this[int index] 
          
    {
             
    set 
             
    {
                
    base[index] = value;
                OnChanged(EventArgs.Empty);
             }

          }

       }

    }


    namespace TestEvents 
    {
       
    using MyCollections;
       
    //侦听者类似于观察者模式中的observers
       class EventListener 
       
    {
          
    private ListWithChangedEvent List;

          
    public EventListener(ListWithChangedEvent list) 
          
    {
             List 
    = list;
             
    // Add "ListChanged" to the Changed event on "List".
             List.Changed += new ChangedEventHandler(ListChanged);      //这里类似于AddObserver(); 
          }


          
    // This will be called whenever the list changes.
          private void ListChanged(object sender, EventArgs e) 
          
    {
             Console.WriteLine(
    "This is called when the event fires."); //这里类似于观察者暴露给subjects 的Update()方法
             Thread.Sleep(5000);
          }


          
    public void Detach() 
          
    {
             
    // Detach the event and delete the list
             List.Changed -= new ChangedEventHandler(ListChanged);      //这里类似于RemoveObserver();
             List = null;
          }

       }


       
    class Test 
       
    {
          
    // Test the ListWithChangedEvent class.
          public static void Main() 
          
    {
          
    // Create a new list.
          ListWithChangedEvent list = new ListWithChangedEvent();       //创建一个subject(observable)

          
    // Create a class that listens to the list's change event.
          EventListener listener = new EventListener(list);             //创建一个观察者

          
    // Add and remove items from the list.
          list.Add("item 1");                                           //subject 的状态发生变化,通知观察者作相应的动作 ListChanged()
          list.Clear();                                                 //同上                            
          listener.Detach();                                            //删除观察者
          }

       }

    }
  • 相关阅读:
    test
    dd 命令 sd卡系统迁移
    关于庖丁分词
    Linux source命令
    Linux系统查看系统是32位还是64位方法总结 in 创新实训
    总结这两天连续干掉的bug In 创新实训 智能自然语言交流系
    穷举法应用——搬砖块
    判断素数类问题汇总
    统计计算学生成绩类问题汇总
    C语言简明数据类型指南
  • 原文地址:https://www.cnblogs.com/flyinthesky/p/1222748.html
Copyright © 2011-2022 走看看