Java:代理模式

来自WHY42

静态代理

Java代理模式是很有用的模式,之前阿里的面试官问我AOP是怎么实现的,说了半天没说清楚,后来才发现原来叫代理模式...代理模式的核心思想是,在方法外面再包装一层,这样执行的时候,当然可以在实际执行的方法前面和后面加别的函数了...

首先要定义一个接口:

interface Handler{
public void handle();
}

然后这个接口有个实现,就略了;关键是还要加一个代理实现:

class Proxy implements Handler{
    HandlerImpl impl = new HandlerImpl();
    
    @Override
    public void handle(){
        //before
        this.impl.handle();
        //after
    }
}

这样实际在使用的过程中,用这个代理而不是直接的实现,就可以在handle处理前后做一些文章了。

动态代理

JDK提供了Proxy接口可以用来实现动态代理,以下是一个例子,先定义接口和实现:

public interface Handler {
    public void handle(String param);
}

public class DefaultHandler implements Handler{
    @Override
    public void handle(String param) {
        System.out.println("Handling=>" + param);
        
    }
}

然后定义代理类:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicHandlerProxy implements InvocationHandler{
    private Object target;
    public Object bind(Object target){
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) 
            throws Throwable {
        System.out.println(proxy.getClass());
        Object result = null;
        this.before();
        result = method.invoke(target, args);
        this.after();
        
        return result;
    }
    
    public void before(){
        System.out.println("----before----");
    }
    
    public void after(){
        System.out.println("----after----");
    }
}
...

DynamicHandlerProxy proxy1 = new DynamicHandlerProxy();
Handler handler = (Handler) proxy1.bind(new DefaultHandler());
handler.handle("My name is Han Meimei!");

Cglib动态代理

JDK提供的代理只能针对于接口,cglib可支持类。定义一个普通类:

public class CglibHandler {
    public void handle(String args){
        System.out.println("This is from a class:" + args);
    }
    
    public void another(){
        System.out.println("another!");
    }
}

下面实现代理:

public class CglibHandlerProxy implements MethodInterceptor{
    private Object target;
    
    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy arg3) throws Throwable {
        Object result = null;
        this.before();
        result = method.invoke(target, args);
        this.after();
        
        return result;
    }
    
    public Object getInstance(Object target){
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        
        return enhancer.create();
    }
    
    public void before(){
        System.out.println("----before----");
    }
    
    public void after(){
        System.out.println("----after----");
    }
}
...
CglibHandlerProxy proxy2 = new CglibHandlerProxy();
CglibHandler handler1 = (CglibHandler)proxy2.getInstance(new CglibHandler());
handler1.handle("Cglib proxy");
handler1.another();