读书人

设计形式:代理模式

发布时间: 2012-10-23 12:12:22 作者: rapoo

设计模式:代理模式

设计形式:代理模式

设计形式:代理模式

?

设计形式:代理模式

?

1,抽象角色

?

设计形式:代理模式

?

设计形式:代理模式

?

设计形式:代理模式

?

设计形式:代理模式

?

?

?

?

?

设计形式:代理模式

?

设计形式:代理模式

?

设计形式:代理模式

?

?

?

?

?

package com.langsin.foo;import java.lang.reflect.Proxy;public class Demo{public static void main(String[] args){// 1.通用的动态代理实现CommonInvocationHandler handler = new CommonInvocationHandler();Foo f;// 2.接口实现1handler.setTarget(new FooImpl());// 方法参数说明:代理类、代理类实现的接口列表、代理类的处理器// 关联代理类、代理类中接口方法、处理器,当代理类中接口方法被调用时,会自动分发到处理器的invoke方法// 如果代理类没有实现指定接口列表,会抛出非法参数异常f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),new Class[] { Foo.class },handler);f.doAction();// 3.接口实现2handler.setTarget(new FooImpl2());f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),new Class[] { Foo.class },handler);f.doAction();}}

??

?

读书人网 >软件开发

热点推荐