程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#事件委托和觀察者模式的比較

C#事件委托和觀察者模式的比較

編輯:關於C#

看了C#的事件,發覺跟學java時見到的觀察者模式相似,網上搜了一下,有總結的帖 子,轉載如下

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved