.NET图像界面按钮的clicked事件浅谈
发送方的对象为了给事件接收对象传递一些额外信息,就需要写一个派生于System.EventArgs的类,封装一些数据。
public class DrawEventArgs:EventArgs
{
privatedouble m_Size;
public DrawEventArgs(doublesize)
{
m_Size = size;
}
public double Size
{
get
{
returnm_Size;
}
}
}
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);
}
}
最新评论