|
namespace Observer
{
//定义一个委托,这里定义了观察者方法的签名,就是一个协议吧
public delegate void NumberEventHandler(object sender,NumberEventArgs e);
//要传递哪些参数到观察者?在这里定义,注意,要继承自EventArgs
public class NumberEventArgs : EventArgs
{
public NumberEventArgs(int number)
{
_number = number;
}
private int _number;
public int Number
{
get { return _number; }
set { _number = value; }
}
}
//观察者模式中的主题
public class Subject
{
//定义一个事件,就是委托的实例了
public event NumberEventHandler NumberReached;
public void DoWithLoop(int number)
{
for (int i = 0; i <= 100; i++)
{
//触发事件的条件到了
if (i == number)
{
NumberEventArgs e = new NumberEventArgs(i);
OnNumberReached(e);
}
}
}
//注意,这个方法定义为保护的,虚拟的,代表子类还可以进行覆盖,改变触发事件的行为
//甚至可以不触发事件 protected virtual void OnNumberReached(NumberEventArgs e)
{
//判断事件是否为null,也就是是否绑定了方法
if (NumberReached != null) NumberReached(this, e);
}
}
public class MainProgram
{
public static void Main()
{
Console.WriteLine("Please Input a 0-100 Number:");
int input = Console.Read();
if (input < 0 || input > 100) { Console.WriteLine("Error"); }
Subject s = new Subject();
//给事件绑定方法,静态的
s.NumberReached += new NumberEventHandler(msgbox_NumberReached);
MainProgram mp = new MainProgram();
//给事件绑定方法,实例方法
s.NumberReached += new NumberEventHandler(mp.console_NumberReached);
s.DoWithLoop(input); Console.Read();
}
void console_NumberReached(object sender, NumberEventArgs e)
{
Console.WriteLine(e.Number.ToString());
}
static void msgbox_NumberReached(object sender, NumberEventArgs e)
{
MessageBox.Show(e.Number.ToString());
}
}
} |
|