Proxy mode (Proxy)----design mode

Proxy pattern (Proxy)----Java design pattern

Proxy mode, see the meaning of the text, is to proxy an object of a class, and check and expand its behavior.

Culture originates from life, and agency is entrusted to carry out some activities that the parties (here refers to ) should do on behalf of the parties .

What it is?

What is proxy mode?

In layman's terms, provides a proxy for objects that use this object to control extensions' usage access to this object. The proxy pattern is used when an object cannot be directly handed over or is not suitable to be called directly by another object.

The proxy pattern is widely used in Java, and there are traces of it everywhere.

Common proxy patterns and structures

The proxy mode is very important in programming thinking. Generally, there are the following proxy modes:

1. Static proxy

insert image description here

Static proxy mainly realizes the same parent class or interface through the proxy object and the proxy object. By aggregating the proxy object into the proxy object, the same behavior method is also defined in the proxy object to encapsulate and expand the original method, and by calling the proxy object The method in implements the method that executes the original object.

2. Dynamic proxy

insert image description here

The dynamic proxy implements the improvement of the static method, that is, the proxy object does not need to inherit the parent class or method, and uses the JDK API. The key is to dynamically generate the proxy object for proxying through the Proxy.newProxyInstance() method.

3.cglib agent

insert image description here

cglib proxies implement proxies to individual objects, without requiring that objects must implement a superclass or interface. However, this kind of agent must refer to the cglib package and implement the agent through the bottom layer of java. It will not be elaborated here. Interested friends can go to the Java design pattern of Mr. Han Shunping at station B to learn!

4. Several variant proxy modes

insert image description here

There are several common variant agent modes, and interested friends can learn about it by themselves~

Static proxy implementation source code

ITeacherDao (parent class)

package com.design_patterns.proxy.staticproxy;

public interface ITeacherDao {
    
    
    //授课的方法
    void teach();
}

TeacherDao (proxy object)

package com.design_patterns.proxy.staticproxy;

public class TeacherDao implements ITeacherDao {
    
    

    @Override
    public void teach() {
    
    
        System.out.println("老师正在授课中...");
    }
}

TeacherDaoProxy (proxy object)

package com.design_patterns.proxy.staticproxy;

//代理对象,静态代理
public class TeacherDaoProxy implements ITeacherDao {
    
    
    private ITeacherDao iTeacherDao;        //定义目标对象,通过接口来进行聚合

    public TeacherDaoProxy(ITeacherDao iTeacherDao) {
    
    
        this.iTeacherDao = iTeacherDao;
    }

    @Override
    public void teach() {
    
    
        System.out.println("代理开始, 完成某些操作......");
        iTeacherDao.teach();
        System.out.println("代理结束......");
    }
}

Client

package com.design_patterns.proxy.staticproxy;

public class Client {
    
    
    public static void main(String[] args) {
    
    
        //创建目标对象
        TeacherDao teacherDao = new TeacherDao();

        //创建代理对象,同时将被代理对象传递给代理对象
        TeacherDaoProxy teacherDaoProxy = new TeacherDaoProxy(teacherDao);

        //通过代理对象,调用到被代理对象的方法
        //即:执行的是代理对象的方法,代理对象再去调用目标对象的方法
        teacherDaoProxy.teach();
    }
}

Dynamic proxy implementation source code

ITeacherDao (parent class)

package com.design_patterns.proxy.dynamicproxy;

//定义接口
public interface ITeacherDao {
    
    
    //定义用于教授的方法
    void teach();
}

TeacherDao (proxy object)

package com.design_patterns.proxy.dynamicproxy;

public class TeacherDao implements ITeacherDao {
    
    

    @Override
    public void teach() {
    
    
        System.out.println("老师正在授课中......");
    }
}

ProxyFactory (proxy factory)

package com.design_patterns.proxy.dynamicproxy;

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

/**
 * 代理对象生产工厂
 */
public class ProxyFactory {
    
    

    //维护一个目标对象,Object
    private Object target;

    //定义构造方法,对 target 进行初始化


    public ProxyFactory(Object target) {
    
    
        this.target = target;
    }

    /**
     * 1. ClassLoader loader:指定当前目标对象使用的类加载器,获取加载器的方法固定
     * 2. Class<?>[] interfaces:目标对象实现的接口类型,使用泛型方法确认类型
     * 3. InvocationHandler h: 事情处理,执行目标对象的方法时,会触发事情处理器方法,
     * 会把当前执行的目标对象方法作为参数传入
     * @return
     */
    public Object getProxyInstance(){
    
    
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
    
    
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
    
                        System.out.println("JDK代理开始~~~~");
                        Object invoke = method.invoke(target, args);
                        System.out.println("JDK代理结束~~~~");
                        return invoke;
                    }
                });
    }
}

Client

package com.design_patterns.proxy.dynamicproxy;

//测试类
public class Client {
    
    
    public static void main(String[] args) {
    
    
        //创建目标对象
        ITeacherDao iTeacherDao = new TeacherDao();

        //给目标对象,创建代理对象
        ITeacherDao proxyInstance = (ITeacherDao)new ProxyFactory(iTeacherDao).getProxyInstance();

        proxyInstance.teach();
    }
}

Summarize

Alright~ Done! This is the proxy mode. What my brother said may be a little rough, hehe, but the principle is basically the same. Friends must understand. Will definitely reply!

Guess you like

Origin blog.csdn.net/weixin_43479947/article/details/108186067