代理,想必大家都应该知道是什么冬冬了,一般的手机产商都有代理商,歌星们都有自己的经纪人,如此这些都可以看作是一种代理模式。下面我选择如下的一种情景来进行讲述:某董事长出差,但是此时公司有个聚会,董事长买单,但是由他的秘书去结帐。我们就权且把这个看作一个代理行为,^_^。
首先我们定义一个接口:商人(merchant),如下所示:
package cn.edu.hust.cm.test;
public interface merchant {
void treat();//商人都要请客吃饭滴,^_^
}
然后我们定义一个类:董事长(director),如下所示:
package cn.edu.hust.cm.test;
public class director implements merchant {
public director() {
}
public void treat() {
system.out.println("董事长请大家吃饭");
}
}
ok,我们现在要给他找代理了,这个代理就是他的秘书(secretary)。这里要涉及到一些代理的机制了。在java中,用来做代理的类一般都要实现invocationhandler,实现它的invoke方法,至于为啥是这样俺也不知道了,^_^。另外,当我们产生一个代理实例(proxy instance)的时候,只要我们通过这个实例调用任何方法,都会导致invoke方法的调用,还是看例子吧,如下所示:
package cn.edu.hust.cm.test;
import java.lang.reflect.invocationhandler;
import java.lang.reflect.method;
public class secretary implements invocationhandler {
public secretary(director director) {
this.director=director;
}
public object invoke(object arg0, method arg1, object[] arg2)
throws throwable {
director.treat();
system.out.println("由秘书结帐咯!");
return null;
}
private director director;
}
现在我们开始构建我们的主体测试代码,如下面的代码所示:
package cn.edu.hust.cm.test;
import java.lang.reflect.invocationhandler;
import java.lang.reflect.proxy;
public class proxytest extends proxy {
public proxytest(invocationhandler arg0) {
super(arg0);
}
public static void main(string[] args) {
director director=new director();
invocationhandler secretary=new secretary(director);
merchant merchant=(merchant)proxy.newproxyinstance(director.getclass().getclassloader(),director.getclass().getinterfaces(),secretary);
merchant.treat();
}
}
运行程序,输出将为:董事长请客
由秘书结帐咯!
这里有几个要说明的地方,如下所述:
1.proxy.newproxyinstance方法的作用,它的作用为某个对象创建一个代理对象,本例中是为director创建了一个代理对象,这个对象属于哪个类呢?我们在程序里面加这样一句system.out.println(merchant.getclass().getname()),输出是$proxy0,呵呵,说明已经产生了一个代理类了,这个类实现了director所属类中的所有接口。
2.代理的实现机制问题。个人觉得这个似乎就是一个事件处理的机制,当代理对象调用某个方法的时候,就会触发相应的invoke方法。因此proxy.newproxyinstance方法的第三个参数就相当于给这个代理对象注册了一个监听器。
proxy类还有几个方法,用法可以查看相应的api文档。
闽公网安备 35060202000074号