Two cases take you to understand the dynamic proxy model

Changed to dynamic proxy mode based on the previous static proxy mode

Case number one

1. Interface

//租房
public interface Rent {
    public void rent();
}

2. Real characters

//房东
public class Host implements Rent {
    public void rent(){
        System.out.println("房东出租房子!");
    }
}

3. Automatically generate proxy class
InvocationHandler will automatically generate proxy class, create a class to implement this class

public class ProxyInvocationHandler implements InvocationHandler {
    //被代理的按口
    private Rent rent;

    public void setRent(Rent rent) {
        this.rent = rent;
    }

    //生成得到代理类
    public Object getProxy() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(), this);
    }

    //处理代理实例。并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质,就是使用反射机制实现!
        seeHouse();
        Object result = method.invoke(rent, args);
        fare();
        return result;
    }

    public void seeHouse() {
        System.out.println("中介带看房子");
    }

    public void fare() {
        System.out.println("收取中介费");
    }
}

4. Customer access

public class Client {
    public static void main(String[] args) {
        //真实角色
        Host host = new Host();
        //代理角色:现在没有
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        //通过调用程序处理角色米处理我们要调用的按口对象!
        pih.setRent(host);
        Rent proxy = (Rent) pih.getProxy();//这里的proxy就是动态生成的,我们并没有写

        proxy.rent();
    }
}

Case 2

1. Interface

public interface UserService{
    void add();
    void delete();
    void update();
    void query();
}

2. Real characters

public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增加了一个用户");
    }
    public void delete() {
        System.out.println("删除了一个用户");
    }

    public void update() {
        System.out.println("修改了一个用户");
    }
    public void query() {
        System.out.println("查询了一个用户");
    }
}

3. Automatically generate proxy class

//等我们会用这个类, 自动生成代理类!
public class ProxyInvocationHandler implements InvocationHandler {
    //被代理的按口
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    //生成得到代理类
    public Object getProxy() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    //处理代理实例。并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质,就是使用反射机制实现
        log(method.getName());//反射得到实现的方法的名字
        Object result = method.invoke(target, args);

        return result;
    }

    public void log(String msg) {
        System.out.println("执行了" + msg + "方法");
    }
}

4. Customer access

public class Client {
    public static void main(String[] args) {
        //真实角色
        UserServiceImpl userService = new UserServiceImpl();
        //代理角色,不存在
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        pih.setTarget(userService);
        //动态生成代理类
        UserService proxy = (UserService) pih.getProxy();
        
        proxy.query();
    }
}

to sum up

Benefits of dynamic agents:

  • Can make the operation of real characters more pure, without having to pay attention to some public business
  • The public will be handed over to the role of agent, realizing the division of business
  • Convenient centralized management when public services are expanded
  • A dynamic agent class agent class is an interface, generally corresponding to a class of business
  • A dynamic proxy class can proxy multiple classes, as long as the same interface is implemented
Published 51 original articles · Likes 73 · Visits 3700

Guess you like

Origin blog.csdn.net/qq_41256881/article/details/105438445