Proxy mode static proxy dynamic proxy proxy mode detailed

Detailed proxy mode

1. Static proxy and dynamic proxy

Agency model

Use a proxy object to wrap the target object. All calls to the target object method must be called through the proxy object, and the proxy class and the proxy class need to implement the same interface (the same method)

Static proxy

The proxy object is determined during compilation, and the proxy class and proxy class have been fixed

Dynamic proxy

The proxy object of the target class is dynamically created through reflection during operation, and the proxy class and the proxy class are not fixed

Class Proxy

//Proxy类中的newProxyInstance方法可以创建代理对象
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)

param1: the class loader of the proxy class
param2: the interface implemented by the proxy class
param3: the object of InvocationHandler

Two, static proxy code implementation

//所要实现的接口
interface ClothFactory {
    
    
    void produceCloth();
}

//被代理类
class NikeClothFactory implements ClothFactory {
    
    

    @Override
    public void produceCloth() {
    
    
        System.out.println("执行被代理类的方法");
    }
}

//代理类
class ProxyClothFactory implements ClothFactory {
    
    

    private ClothFactory factory; //拿被代理的类对象赋值(多态)

    public ProxyClothFactory(ClothFactory factory) {
    
    
        this.factory = factory;
    }

    @Override
    public void produceCloth() {
    
    
        factory.produceCloth(); //调用被代理对象的方法
    }
}

//测试类
public class StaticProxy {
    
    
    public static void main(String[] args) {
    
    

        //创建被代理类的对象
        ClothFactory nike = new NikeClothFactory();

        //创建代理类的对象
        ClothFactory proxyClothFactory  = new ProxyClothFactory(nike);
        //执行的是被代理类的方法
        proxyClothFactory.produceCloth(); //执行被代理类的方法

        //特点:代理类和被代理类在编译期间就确定下来了
    }
}

Three, dynamic proxy code implementation

//要实现的接口
interface Human {
    
    
    void eat(String food);
}

//创建不同代理类的工厂
class ProxyFactory{
    
    

    /**
     * @param obj 被代理类的对象
     * @return 动态创建的对应的代理类对象
     */

    public static Object getProxyInstance(Object obj) {
    
    

        //创建一个类实现了InvocationHandler接口,并创建此类对象
        InvocationHandler handler = new MyInvocationHandler(obj);

        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
                obj.getClass().getInterfaces(), handler);
    }
}

//实现InvocationHandler接口
class MyInvocationHandler implements InvocationHandler {
    
    

    //当代理类对象调用某个方法时,就会自动的调用如下方法
    //故应当将被代理类要执行的方法声明在invoke()中

    /**
     * @param method 被代理类要执行方法的Method类型
     * @param args 被代理类要执行方法的参数
     * @return 被代理类要执行方法的返回值
     */
    @Override
    public Object invoke(Object proxy, Method method,
                         Object[] args) throws Throwable {
    
    
        Object value = method.invoke(obj, args);
        return value;
    }

    private Object obj; //使用被代理类的对象赋值

    public MyInvocationHandler(Object obj) {
    
    
        this.obj = obj;
    }

}

//被代理类
class SuperMan implements Human {
    
    

    @Override
    public void eat(String food) {
    
    
        System.out.println("我喜欢吃" + food);
    }
}

//测试类
public class ProxyTest {
    
    
    public static void main(String[] args) {
    
    
        //被代理类的对象
        SuperMan superMan = new SuperMan();
        //代理类的对象
        Human proxyInstance = (Human)ProxyFactory.getProxyInstance(superMan);
        proxyInstance.eat("土豆"); //我喜欢吃土豆
    }
}

Guess you like

Origin blog.csdn.net/weixin_49343190/article/details/109543603
Recommended