首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计形式:代理模式

2012-10-23 
设计模式:代理模式??1,抽象角色????????????????package com.langsin.fooimport java.lang.reflect.Proxy

设计模式:代理模式

设计形式:代理模式

设计形式:代理模式

?

设计形式:代理模式

?

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();}}

??

?

热点排行