在大话设计模式中有一个模式叫做观察者模式,里面涉及到了委托和事件的使用。虽然在一开始学习C#的时候有接触过,但是还是很陌生,稳扎稳打,才能平步青云。所以让我们来熟悉吧!
下面结合实例来讲解两者的关系
class Program { static void Main(string[] args) { Cat cat=new Cat ("Tom"); Mouse mouse1=new Mouse ("Jerry"); Mouse mouse2=new Mouse("Jack"); cat.CatShout +=new Cat.CatShoutEventHandler(mouse1.Run ); cat .CatShout +=new Cat.CatShoutEventHandler (mouse2.Run ); //表示将Mouse的Run方法通过实例化委托Cat.CatShoutEventHandler登记到Cat事件CatShout当中。 cat.Shout (); Console.Read (); } } class Cat { private string name; public Cat(string name) { this.name = name; } public delegate void CatShoutEventHandler(); //声明委托CatShoutEventHandle public event CatShoutEventHandler CatShout; //声明事件CatShout,它的事件类型是委托CatShoutEventHandler public void Shout() { Console.WriteLine ("瞄,我是{0}。",name); if (CatShout !=null ) { CatShout (); //表明当执行Shout()方法时,如果CatShout中有对象登记事件,则执行CatShout() } } class Mouse { private string name; public Mouse (string name) { this.name =name; } public void Run(object sender,CatShoutEventArge args) { Console.WriteLine ("老猫{0}来了,{1}快跑!",args.Name ,name ); } }
关键点在这cat.CatShout += Cat.CatShoutEventHandler(mouse1.Run)通过CatShoutEventHandler将Mouse和Cat进行了交互绑定,当cat.CatShout被调用后,执行CatShout事件,通知老鼠猫来了,通知后该事件被委托给mouse1.Run进行处理(赶紧跑)。
增加一个类CatShoutEventArgs,让它继承EventArgs,EventArgs是包含事件数据的类的基类。这个的目的就是在CatShout事件触发时,需要传递Cat对象的名字。
public class CatShoutEventArge:EventArgs { private string name; public string Name { get {return name;} set { name = value; } } }
改写Cat类的代码,对委托CatShoutEventHandler进行重新定义。增加两个参数,第一个参数Object对象sender是指向发送通知的对象,而第二个参数CatShoutEventArgs的args,包含了所有通知者需要的附件信息,这里是老猫的名字信息。
class Cat { private string name; public Cat(string name) { this.name = name; } public delegate void CatShoutEventHandler(object sender,CatShoutEventArge args); //声明委托CatShoutEventHandle public event CatShoutEventHandler CatShout; //声明事件CatShout,它的事件类型是委托CatShoutEventHandler public void Shout() { Console.WriteLine ("瞄,我是{0}。",name); if (CatShout !=null ) { CatShoutEventArge e = new CatShoutEventArge(); e.Name = this.name; CatShout(this, e); } } }
class Mouse { private string name; public Mouse (string name) { this.name =name; } public void Run(object sender,CatShoutEventArge args) { Console.WriteLine ("老猫{0}来了,{1}快跑!",args.Name ,name ); }