java design pattern (1) agent model

1. Static proxy

静态代理It is required that both the proxy class and the proxy class implement the same interface or inherit the same parent class. The specific idea is: set a target interface attribute in the proxy class and provide a construction method with the target interface as a parameter, and realize the target interface at the same time. Then pass the proxy object in the form of polymorphism to complete the creation of the proxy object.
优点:It is possible to extend the target function without modifying the function of the target object, while avoiding the external exposure of the business logic of the actual object.
缺点:Due to the need to implement interfaces, the code is cumbersome and difficult to maintain.

2. Dynamic proxy

There are two main types of dynamic agents: JDK动态代理(接口代理)andCglib动态代理(子类代理)

1. JDK dynamic proxy

JDK动态代理The proxy class does not necessarily need to implement the target interface, but the target class must implement the target interface. The specific ideas are:
a. Obtain Class objects through java reflection loading target interface.
b. Obtain the constructor through the handler handle (InvocationHandler interface).
c. Then inject the target object into the handler handle in the form of polymorphism.
d. Construct a proxy class object instance based on the proxy processor handle, and force the completion of the proxy to the target interface.

    @Test
    public void t() throws Exception{
    
    
        Subject realSubject = new RealSubject();
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true"); 
        //1.0 获取代理类的类对象,主要设置相同的ClassLoader去加载目标类实现的接口Subject类
        Class<?> proxyClass = Proxy.getProxyClass(Client.class.getClassLoader(), new Class[]{
    
    Subject.class});
        //2.0 得到代理类后,就可以通过代理类的处理器句柄来得到构造器
        final Constructor<?> con = proxyClass.getConstructor(InvocationHandler.class);
        //3.0 获取具体执行方法的句柄处理器,目的通过构造器传入被代理目标类对象,注入到代理类处理器句柄中进行代理调用
        final InvocationHandler handler = new DynamicProxy(realSubject);
        //4.0 通过构造器创建代理类对象
        Subject subject = (Subject)con.newInstance(handler);
        //5.0 最后调用方法
        subject.hello("proxy");

Note: Here the DynamicProxy class implements the InvocationHandler interface and overrides the invoke method.
In addition, the reason why JDK dynamic proxy can only implement proxy for interfaces is because of Java 单继承机制. The proxy class has inherited the java.lang.reflect.Proxy class and implemented the interface we passed in. Java does not allow multiple inheritance classes but allows multiple implementation interfaces, so function expansion can only be achieved by implementing the target interface.
优点:Solve the problem of redundant proxy implementation in static proxy.
缺点:Cannot proxy classes that do not implement interfaces.
Reference link: JDK dynamic proxy principle analysis

2. Cglib dynamic proxy

Cglib动态代理Realize the proxy of the object through the underlying bytecode enhancement technology. The specific idea is: create a proxy subclass by inheriting the proxy class, implement the MethodInterceptor interface, intercept all parent class method calls, override the intercept method, and then complete the dynamic proxy through the callback method of the Enhancer class.
优点:You can also dynamically proxy classes that do not implement interfaces.
缺点:The technology is more complicated...
Note: It is precisely because the Cglib dynamic proxy is realized by creating subclass objects, Cglib cannot process the method modified by the final keyword.

Guess you like

Origin blog.csdn.net/m0_46550452/article/details/106751119