-
C#事件委托 和 观察者模式之比较
namespace MyCollections
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// A class that works just like ArrayList, but sends event
// notifications whenever the list changes.
public class ListWithChangedEvent: ArrayList
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// An event that clients can use to be notified whenever the
// elements of the list change.
public event ChangedEventHandler Changed; //事件相当于实做Notify()函数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Invoke the Changed event; called whenever list changes
protected virtual void OnChanged(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Changed != null) //这里表示subjects状态有变化,需要通知observers.
Changed(this, e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Override some of the methods that can change the list;
// invoke event after each
public override int Add(object value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i = base.Add(value);
OnChanged(EventArgs.Empty);
return i;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Clear()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.Clear();
OnChanged(EventArgs.Empty);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override object this[int index]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base[index] = value;
OnChanged(EventArgs.Empty);
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace TestEvents
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
using MyCollections;
//侦听者类似于观察者模式中的observers
class EventListener
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private ListWithChangedEvent List;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public EventListener(ListWithChangedEvent list)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
List = list;
// Add "ListChanged" to the Changed event on "List".
List.Changed += new ChangedEventHandler(ListChanged); //这里类似于AddObserver();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// This will be called whenever the list changes.
private void ListChanged(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("This is called when the event fires."); //这里类似于观察者暴露给subjects 的Update()方法
Thread.Sleep(5000);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Detach()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Detach the event and delete the list
List.Changed -= new ChangedEventHandler(ListChanged); //这里类似于RemoveObserver();
List = null;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
class Test
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Test the ListWithChangedEvent class.
public static void Main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Create a new list.
ListWithChangedEvent list = new ListWithChangedEvent(); //创建一个subject(observable)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create a class that listens to the list's change event.
EventListener listener = new EventListener(list); //创建一个观察者
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Add and remove items from the list.
list.Add("item 1"); //subject 的状态发生变化,通知观察者作相应的动作 ListChanged()
list.Clear(); //同上
listener.Detach(); //删除观察者
}
}
}
-
相关阅读:
不错的电影(先收藏着)
getchar() getch() getche() gets() puts() scanf()的用法及区别
java反射获取字段的属性值,以及为字段赋值等方法
oracle将查询结果横转纵
关于MySQL 的LEFT JOIN ON的问题
MySQL表名和数据库关键字相同解决办法
ajax 后台正常执行 错误类型却是404
BIT 树状数组 详解 及 例题
HDU 2689 Sort it (树状数组)
HDU Cow Sorting (树状数组)
-
原文地址:https://www.cnblogs.com/flyinthesky/p/1222748.html
Copyright © 2020-2023
润新知