一、定义 :
委托是对函数的封装,可以当作给方法的特征指定一个名称。事件是委托的一种特殊形式,当发生有意义的事情时,事件对象处理通知过程。
示例:猫来了,老鼠大喊:老猫来了,快跑!
C#实现代码:
using System;
class Cat
{
private string name;
public Cat(string name)
{
this.name=name;
}
public delegate void CatShoutEventHander();//声明委托
public event CatShoutEventHander CatShout;//
public void Shout()
{
Console.WriteLine("miao ,i am {0}",name);
if(CatShout!=null)
{
CatShout();//触发事件,如果CatShout中有对象登记,执行所有登记过的函数 ;
}
}
}
class Mouse
{
private string name;
public Mouse(string name)
{
this.name=name;
}
public void run()
{
Console.WriteLine("the cat has arrived,{0},go go go!",name);
}
}
public class Client
{
public static void Main(string[] args)
{
Cat cat=new Cat("Tom");
Mouse mouse1=new Mouse("Jerry");
Mouse mouse2=new Mouse("Jack");
cat.CatShout+=new Cat.CatShoutEventHander(mouse1.run);
cat.CatShout+=new Cat.CatShoutEventHander(mouse2.run);//实例化run方法形成一个委托然后登记到CatShout中
cat.Shout();
Console.WriteLine();
}
}
总结:delegate定义一个委托(“形成一种类型”),event定义一个对应委托类型的事件(“存贮相应类型委托的容器”),然后场景类中向容器中添加实例化为委托类型的相应函数。
Java代码:
委托类:
package Cat;
import java.lang.reflect.Method;
public class Delegate {
private Object obj;
private String methodName;
private Object[] methodParameter;
private Class<?>[] methodType;
public Delegate() {
}
public Delegate(Object obj,String methodName,Object...methodParameters) {
//把委托者的方法形成一个Delegate,只要调用Delegate的invoke方法即可,封装方法需要的信息有:对象
//、方法名、方法参数
this.obj=obj;
this.methodName=methodName;
this.methodParameter=methodParameters;
int len=methodParameters.length;
this.methodType=new Class[len];
for(int i=0;i<len;++i)methodType[i]=methodParameters[i].getClass();
}
public void invoke() throws NoSuchMethodException, ReflectiveOperationException {
Method m=obj.getClass().getDeclaredMethod(methodName, methodType);
m.invoke(obj,methodParameter);
}
}
事件类:
package Cat;
import java.util.Vector;
public class Event {
private Vector<Delegate> vs=new Vector<Delegate>();
public void invoke() throws NoSuchMethodException, ReflectiveOperationException {
for(Delegate d:vs) {
d.invoke();
}
}
public void addDelegate(Delegate delegate) {
System.out.println(delegate.toString());
vs.add(delegate);
}
public void delDelegate(Delegate delegate) {
vs.remove(delegate);
}
}
被委托者:Tom猫:
package Cat;
public class Cat {
private String name;
private Event event;
public Cat(String name) {
this.name=name;
event=new Event();
}
public void addDelegate(Object obj,String methodName,Object...methodParameters) {
Delegate d=new Delegate(obj, methodName, methodParameters);
//System.out.println(obj.toString()+" "+methodName+" "+methodParameters.length);
event.addDelegate(d);
}
public void delDelegate(Object obj,String methodName,Object...methodParameters) {
Delegate d=new Delegate(obj, methodName, methodParameters);
event.delDelegate(d);
}
public void action() throws NoSuchMethodException, ReflectiveOperationException {
System.out.println("我是"+name+",我来抓老鼠了!");
event.invoke();
}
}
委托者:两只小老鼠:
package Cat;
public class Mouse1 {
private String name;
public Mouse1(String name) {
this.name=name;
}
public void shout(String name) {
System.out.println(name+"来了,我是"+this.name+",我要快跑!");
}
}
package Cat;
public class Mouse2 {
private String name;
public Mouse2(String name) {
this.name=name;
}
public void speak(String name) {
System.out.println(name+"来了,我是"+this.name+",我要快跑!");
}
}
场景类:
package Cat;
public class Client {
public static void main(String[] args) throws NoSuchMethodException, ReflectiveOperationException {
Cat cat=new Cat("Tom");
Mouse1 m1=new Mouse1("Jerry");
Mouse2 m2=new Mouse2("Jack");
cat.addDelegate(m1,"shout","Tom");
cat.addDelegate(m2,"speak","Tom");
cat.action();
}
}
运行结果:
The end;