using System.Windows.Forms;
namespace ObserverModel
{
public class BankAccount
{
public event AccountChangeEventHander accountChange;
public void withDraw(string userName)
{
UserValueObject args = new UserValueObject();
args.name = userName;
onAccountChange(args);
}
protected virtual void onAccountChange(UserValueObject args)
{
if(accountChange!=null)
{
accountChange(null,args);
}
}
}
public delegate void AccountChangeEventHander(object sender, UserValueObject args);
public class Emailer
{
public void update(object sender, UserValueObject args)
{
MessageBox.Show(args.name);
}
}
public class UserValueObject
{
public string name = "Anders";
}
public class app
{
static void Main()
{
BankAccount bankAccount = new BankAccount();
Emailer emailer = new Emailer();
bankAccount.accountChange += new AccountChangeEventHander(emailer.update);
bankAccount.withDraw("Anders.lu");
}
}
}
.net 事件模式 访问者模式
最新推荐文章于 2022-04-07 10:14:45 发布