但是事件並不局限於圖像界面,也可能是滿足程序的某個邏輯判斷觸發了事件。引發事件的對象叫做事件發送方,捕獲事件並對其做出響應的對象叫做事件接收方。但是事件發送方並不知道哪個對象或者方法會處理它引發的事件,所以需要在事件發送方和事件接收方之間存在一媒介,明確某個對象或者某個類型的某個方法會對這個事件進行響應。.NET用委托作為事件發送方與事件接收方之間的媒介,委托只有一個簽名,只要方法簽名與委托簽名匹配的方法,都可以聲明自己對這個委托類型的事件的感興趣,接收並處理它。
事件發送方的對象為了給事件接收對象傳遞一些額外信息,就需要寫一個派生於System.EventArgs的類,封裝一些數據。
復制代碼 代碼如下:
public class DrawEventArgs:EventArgs
{
privatedouble m_Size;
public DrawEventArgs(doublesize)
{
m_Size = size;
}
public double Size
{
get
{
returnm_Size;
}
}
}
下面聲明一委托,該委托有兩個參數,一個是參數代表事件的發送方,另一個是參數是該事件封裝的數據。
public delegate void ShapeSizeChanged(objectsender,DrawEventArgs e);
ShapeSizeChanged的實例可以綁定到任何和它方法簽名匹配的方法上。
下面是自定義事件委托,
public event ShapeSizeChanged SizeChanged;
下面是一個負責引發事件的類型代碼,
復制代碼 代碼如下:
public class DrawManager
{
public event ShapeSizeChangedSizeChanged;
protectedvoid OnSizeChanged(DrawEventArgse)
{
ShapeSizeChangedtemp = SizeChanged;
//是否有委托與該事件關聯
if(temp != null)
{
temp(this,e);
}
}
public void SizeChange(doublesize)
{
DrawEventArgse = new DrawEventArgs(size);
OnSizeChanged(e);
}
}
而後定義兩個監聽事件的類型,
復制代碼 代碼如下:
public class Square
{
publicSquare(DrawManager drawManager)
{
//drawManager.SizeChanged+= DrawSquare;
//把事件關聯到委托上
drawManager.SizeChanged += new ShapeSizeChanged(DrawSquare);
}
public void DrawSquare(objectsender, DrawEventArgs e)
{
Console.WriteLine(string.Format("TheSquare'Length = {0}", e.Size));
}
public void Detach(DrawManagerdrawManager)
{
//drawManager.SizeChanged-= DrawSquare;
//解除事件和委托的關聯
drawManager.SizeChanged -= new ShapeSizeChanged(DrawSquare);
}
}
public class Rectangle
{
publicRectangle(DrawManager drawManager)
{
drawManager.SizeChanged +=DrawRectangle;
}
public void DrawRectangle(objectsender, DrawEventArgs e)
{
Console.WriteLine(string.Format("TheRectangle'length={0} and width={1}.",e.Size*2,e.Size));
}
public void Detach(DrawManagerdrawManager)
{
drawManager.SizeChanged -=DrawRectangle;
}
}
測試代碼,
復制代碼 代碼如下:
class Program
{
static void Main(string[]args)
{
DrawManagerdrawManager = new DrawManager();
Rectanglerect = new Rectangle(drawManager);
Squaresquare = new Square(drawManager);
//引發事件
drawManager.SizeChange(5);
//解除監聽事件
square.Detach(drawManager);
drawManager.SizeChange(10);
Console.ReadLine();
}
}
/*運行結果
The Rectangle'length=10 and width=5.
The Square'Length = 5
The Rectangle'length=20 and width=10.
*/
.NET中的事件模式和觀察者模式非常相似,也可謂是觀察者模式在.NET下的進化版吧,下面用觀察者模式實現上面功能以做對比,首先定義兩個接口,IObserver和IObservable,如下
復制代碼 代碼如下:
public interface IObserver
{
voidNotify(DrawEventArgs e);
}
public interface IObservable
{
voidRegister(IObserver observer);
voidUnRegister(IObserver observer);
}
下面是改寫後的兩個觀察者類,
復制代碼 代碼如下:
public class NewRectangle:IObserver
{
privateObserverManager m;
publicNewRectangle(ObserverManager oManager)
{
m=oManager;
oManager.Register(this);
}
public void Notify(DrawEventArgse)
{
Console.WriteLine(string.Format("TheRectangle'length={0} and width={1}.", e.Size * 2, e.Size));
}
public void Detach()
{
m.UnRegister(this);
}
}
public class NewSquare:IObserver
{
privateObserverManager m;
publicNewSquare(ObserverManager oManager)
{
m=oManager;
oManager.Register(this);
}
public void Notify(DrawEventArgse)
{
Console.WriteLine(string.Format("TheSquare'Length = {0}.", e.Size));
}
public void Detach()
{
m.UnRegister(this);
}
}
下面是負責通知觀察者的類型,
復制代碼 代碼如下:
public class ObserverManager:IObservable
{
protectedArrayList arrList;
publicObserverManager()
{
arrList = newArrayList();
}
public void Register(IObserverobserver)
{
arrList.Add(observer);
}
public void UnRegister(IObserverobserver)
{
if(arrList.Contains(observer))
{
arrList.Remove(observer);
}
}
public void NotifyObservers(doublesize)
{
DrawEventArgse = new DrawEventArgs(size);
foreach(IObserver observer inarrList)
{
observer.Notify(e);
}
}
public void SizeChanged(doublesize)
{
NotifyObservers(size);
}
}
下面是調用代碼,
復制代碼 代碼如下:
static void Main(string[]args)
{
ObserverManageroManager = new ObserverManager();
NewRectanglerect = new NewRectangle(oManager);
NewSquaresquare = new NewSquare(oManager);
oManager.SizeChanged(5);
square.Detach();
oManager.SizeChanged(10);
Console.ReadLine();
}
最好運行下代碼,這樣可以更easy的理解這兩種模式微妙的差別了。
對事件來說,還可以顯式的用add和remove編寫事件訪問器,事件訪問器通常有編譯器生成,所以可以顯式的用事件訪問器修改DrawManager類型,
復制代碼 代碼如下:
public class DrawManager
{
privateevent ShapeSizeChangedm_SizeChanged;
privatereadonly objectm_lock = new object();
public event ShapeSizeChangedSizeChanged
{
add
{
lock(m_lock)
{
m_SizeChanged += value;
}
}
remove
{
lock(m_lock)
{
m_SizeChanged -= value;
}
}
}
protectedvoid OnSizeChanged(DrawEventArgse)
{
ShapeSizeChangedtemp = m_SizeChanged;
//是否有委托與該事件關聯
if(temp != null)
{
temp(this,e);
}
}
public void SizeChange(doublesize)
{
DrawEventArgse = new DrawEventArgs(size);
OnSizeChanged(e);
}
}