看了1月的程序员杂志,看到动态代理的讲述,写下如下学习笔记,其中demo做了改动,更好体现动态代理的概念。
一:动态代理实践
1.接口类:Foo.java
package dymaticproxy;
/**
* <p>Title: 高层提供的服务接口</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: huawei</p>
* @author zhaolh
* @version 1.0
*/
public interface Foo {
void doAction();
}
2.接口的实现1
package dymaticproxy;
/**
* <p>Title:高层提供服务接口的具体实现 </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author zhaolh
* @version 1.0
*/
public class FooImpl implements Foo {
public FooImpl() {
}
public void doAction() {
/**@todo Implement this dymaticproxy.Foo method*/
//throw new java.lang.UnsupportedOperationException("Method doAction() not yet implemented.");
System.out.println("in FooImp1.doAction()");
}
}
3.接口的实现2
package dymaticproxy;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author zhaolh
* @version 1.0
*/
public class FooImpl2 implements Foo {
public FooImpl2() {
}
public void doAction() {
/**@todo Implement this dymaticproxy.Foo method*/
//throw new java.lang.UnsupportedOperationException("Method doAction() not yet implemented.");
System.out.println("in FooImp2.doAction()");
}
}
4.处理器类
package dymaticproxy;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;
/**
* <p>Title: 动态代理类的调用处理器</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author zhaolh
* @version 1.0
*/
public class CommonInvocationHandler implements InvocationHandler {
//动态执行对象,需要回调的对象
private Object target;
//支持构造子注射
public CommonInvocationHandler() {
}
//支持构造子注射
public CommonInvocationHandler(Object target) {
setTarget(target);
}
/**
* 采用setter方法注射
* @param target
*/
public void setTarget(Object target)
{
this.target=target;
}
/**
* 调用proxy中指定的方法method,并传入参数列表args
* @param proxy 代理类的类型,例如定义对应method的代理接口
* @param method 被代理的方法
* @param args 调用被代理方法的参数
* @return
* @throws java.lang.Throwable
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
/**@todo Implement this java.lang.reflect.InvocationHandler method*/
//throw new java.lang.UnsupportedOperationException("Method invoke() not yet implemented.");
return method.invoke(target,args);
}
}
5.动态代理示范程序
package dymaticproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author zhaolh
* @version 1.0
*/
public class Demo {
public static void main(String[] args) {
//1.通用的动态代理实现
CommonInvocationHandler handler = new CommonInvocationHandler();
Foo f;
//2.接口实现1
handler.setTarget(new FooImpl());
//方法参数说明:代理类、代理类实现的接口列表、代理类的处理器
//关联代理类、代理类中接口方法、处理器,但代理类中接口方法被调用时,会自动分发到处理器的invoke方法
//如果代理类没有实现指定接口列表,会抛出非法参数异常
f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);
f.doAction();
//3.接口实现2
handler.setTarget(new FooImpl2());
f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);
f.doAction();
}
}
二:动态代理的总结
代理类(可以单个接口和类)、代理类接口(可以是多个接口)、处理器构成一种动态的关系,创建和运行期都可以随意改变的。
例如上例中Proxy.newProxyInstance创建的(Foo.class.getClassLoader()类型)实例f,可以实现new Class[] { Foo.class }中指定接口,并和handler关联。所有通过f的调用接口(new Class[] { Foo.class })中方法(doAction())都被转发到invoke()中动态调用target的doAction()实现。
总之动态代理提供一种动态代理类,它的实例可以实现任意的业务接口,并且可以在运行时决定某个实例实现那个接口。同时可以实现对特有调用方法的拦截。
分享到:
相关推荐
拨号界面 1)坐标调整wgui.h 设置断点,跟踪 IdleScreenDigitHandler()--
这是android application development 中用到的源码-dialing-example,在书中有详细的分析,大家看着用吧
A Phone Dialing program that play both DTMF Tones and MF Tones using wav files. It does not use A real Phone.
螺光宽带拨号客户端是专门针对国内广大宽带拨号用户定制的免费拨号软件。具有绿色无插件,拨号速度快,占用资源少,并可以设置自动运行,自动拨号,断线后自动重连等功能。 螺光宽带拨号客户端内置的辅助功能,您...
代码:稍微修改了ispeech.org的例子程序,把英文语言,修改为中文语言。 功能:使用ispeech.org的...注意:编译项目的时候,你需要去ispeech.org网站上免费申请API Key,替换掉iSpeechSample.java里面的_APIKey。
DWDS / Dialing Concordance(DDC)-语料库语言学家的索引和搜索工具的集合
顺序记录了正连接上的通话,这些通话包 括:ACTIVE,DIALING,ALERTING,HOLDING,INCOMING,WAITING等状态的连接。GSMCallTracker将 这些连接分为了三类别进行管理: RingingCall: INCOMING ,WAITING ...
For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can ...
脉冲拨号 让任何旋转电话都响起来,并通过模拟电话交换机使之栩栩如生。
WhereIsIP为了找到更好的dialing
SERIES V: DATA COMMUNICATION OVER THE TELEPHONE NETWORK;Serial asynchronous automatic dialling and control
The principle of electric telephony Signaling Direct links Manual exchanges ...Pulse dialing Tone dialing The General Switched Telephone Network Multiplexing In-band vs. out of band signaling
With this application installed you will hear voice, telling what number on keypad was pushed, during dialing
ABPLC例程源码Dialing a Cell Phone with a Micrologix 1500 using a Modem Attached本资源系百度网盘分享地址
NetWorx 4.9.7 带宽测量工具...* Added dialing a connection on start-up (that can be used to establish an initial dial-up, ADSL or mobile broadband connection). * Several minor user interface enhancements.
1. dialing call 拨打一路电话,对方尚未接听,主叫方等待被交接听的时候的状态即为dialing状态 2. active call 激活通话:双方正在进行的正常的通话 3. hold call 保持通话:即将当前通话挂起,双方不管主叫还是...
开源项目-IftekherSunny-go_country.zip,Country is the package that helps you to get country name and dialing code by the country ISO 3166-1 Alpha-2 code
> ABD (Abbreviated Dialing) 缩位拨号 > ABF (Adaptive BeamForming) 自适应天线波束赋形技术 > ABM (Asynchronous Balanced Mode) 异步平衡方式 > ABS (Alternate Billing Service) 可选择记帐业务 > A...
and add some Bluetooth headset functions, that is, a cell phone can control another cell phone to achieve dialing and hang up functions
含有 NoiseX-92噪声库-完整版(wav格式): ... Clap N87: Snore N88: Click N88-N90: Laugh N91-N92: Yawn N93: Cry N94: Shower N95: Tooth brushing N96-N97: Footsteps N98: Door moving N99-N100: Phone dialing