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();                                            //删除观察者
          }

       }

    }
  • 相关阅读:
    利用“通过'反射方式'创建的对象”来创建对象的性能问题?
    Linq 学习笔记(一)
    说说数据类型转换帮助类
    使用公共静态属性的方式来代替公共静态字段(C#)
    支持差异数据保存的数据库实体类设计(二)(续)
    编程杂记
    Linq 学习笔记(二)
    UI版本WinDBG常用命令
    Oracle数据库卸载
    SAS框架问世
  • 原文地址:https://www.cnblogs.com/flyinthesky/p/1222748.html
Copyright © 2011-2022 走看看