Design Patterns dynamic proxy mode

Foreword

Each agent class can only serve as an interface, so that the program would have a lot of development in the proxy class.
So we will try to complete all of the proxy function through a proxy class, then we need to use dynamic proxies

In Java in order to achieve dynamic proxy mechanism, it is necessary java.lang.reflect.InvocationHandlerinterfaces and java.lang.reflect.Proxysupport classes


java.lang.reflect.InvocationHandlerThe interface is defined as follows:

//Object proxy:被代理的对象  
//Method method:要调用的方法  
//Object[] args:方法调用时所需要参数  
public interface InvocationHandler {  
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;  
}  

java.lang.reflect.ProxyClass is defined as follows:

//CLassLoader loader:类的加载器  
//Class<?> interfaces:得到全部的接口  
//InvocationHandler h:得到InvocationHandler接口的子类的实例  
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException  

Dynamic proxy class can only proxy interfaces (abstract classes are not supported), the proxy class needs to implement InvocationHandler class, implement invoke methods. The invoke method returns a value needs to be called when the invoke method is to call all the methods are proxy interface is a proxy class that implements the interface

Guess you like

Origin www.cnblogs.com/loveer/p/11300416.html