Explain the proxy mode in another way~

Insert picture description here

Speaking of the agency model, I considered him three-in and three-out. I have written at least two articles, but when I read it again, I always feel that it is not easy to understand. Today, I will come to a real case of my own through the summer vacation. The experience of finding a house for internship will explain the agency model.

Static proxy The
proxy mode can be roughly divided into two types, one is static proxy, and the other is dynamic proxy. Static proxy is relatively simple. Let's first come to a renting scene of my summer vacation~

During the summer vacation, I came to Hangzhou by myself and found an internship. Then I asked HR which housing complex nearby is better (the first thing is to rent a house ), and then he said "Fuding Jiayuan", then I came When I arrived in Fuding, there was a small room next to it, which seemed to be the place where the temporary residence permit was registered. I asked him, is there a house for rent here? Then he called me, called an aunt, and showed me the houses. Where did these houses come from? (The second thing is that the landlord registered the house with the agent ). It turned out to be the landlord’s house, but the agent. I took me to see it, and then I saw a lot of houses (the third thing is to follow the agent to see the house ), finally saw a small room, I was very satisfied, and then signed ( finally I successfully rented the house )

The first thing , as can be seen from the simple description above, all of this stems from one thing you want to do, which is to rent a house, that is to say, you have to have demand.

// 首先要有一个需求,也就是要租房子,这个需求不用实现,让房东和代理去实现
public interface Rent {
    public void rent();
}

The second thing is , if there is a demand for renting a house, do you have to have a landlord to rent out the house, or else there is no rent.

// 房东实现Rent接口,做出租房子这件事
public class Host implements Rent {
    public void rent() {
        System.out.println("房东出租房子");
    }
}

The third thing is that if the landlord wants to rent the house, can the agent help the landlord? The emergence of the agent is mainly to complete some of the landlord’s chores, such as looking at the house and "flickering", and the landlord only needs to rent the house. (The landlord’s affairs are relatively simple).

public class Proxy implements Rent{
    private Host host;

    public void setHost(Host host) {
        this.host = host;
    }

    public void rent() {
        //这里是代理帮助房东出租房子
        host.rent();
    }
    public void watch() {
		System.out.println("看房子,一家一家又一家,我中介从不怕累。。。");
	}
}

The last successful doll

public class Client {
    public static void main(String[] args) {
        Host host = new Host();
        Proxy proxy = new Proxy();
        proxy.setHost(host);
        proxy.rent();
    }
}

After this wave of one-for-five exchanges, the enemy’s crystal was finally destroyed, and no loss was made. At this time, the landlord achieved the goal and successfully rented out the house. The intermediary (agent) achieved the goal and successfully received the intermediary fee. I also achieved it. Purpose, succeeded in being the intermediary routine (found the favorite house).

Smart kids will definitely find a problem. There is only one landlord here, which is Host, and the intermediary is also a Proxy. Is it possible that a smart agent only acts as a landlord’s agent? I don’t think it will. There are spare tires for cars running at high speeds. So the question is, how can an agent act for multiple landlords (with multiple boats)? This is a bit difficult, and it’s not difficult to think about it. Isn’t it enough for me to combine multiple landlords with one agent? (Life experience Rich, you are right, but the program is not easy to write...) In this way, whenever a new landlord wants to rent out a house, he can register with the agency here, there is such a slight dynamic Feel it, which is the dynamic agent that I will talk about next.

Dynamic proxy The great
thing about dynamic proxy is the role of proxy, which is implemented here by the dynamic proxy provided by JDK. This method is based on interfaces, and the other is CGlib, which is based on classes.
The first thing is the same rental house, this thing remains the same

// 出租房子的需求
public interface Rent {
    public void rent();
}

The second thing is that the landlord rents out the house.

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

The third thing is to generate a proxy class, which is the core of the dynamic proxy, which is implemented through the Proxy class and the InvocationHandler interface provided by the JDK.

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
 
//该类会自动生成代理类
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);
    }
 
    //处理代理程序,并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质,用反射机制
        seeHost();//被扩展的功能可以直接加到这
        Object result = method.invoke(rent,args);
        return result;
    }
 
    //在这里可以扩展功能
    public void seeHost() {
        System.out.println("代理带看房子");
    }
}

In the end, the house was successfully rented


import com.sun.org.apache.regexp.internal.RE;
 
public class Client {
    public static void main(String[] args) {
 
        //这是真实角色,也就是我们的房东
        Host host = new Host();
 
        //创建代理调用处理角色
        ProxyInvocationHandler proxyInvocationHandler = new ProxyInvocationHandler();
 
        //通过调用程序处理角色来处理我们要调用的接口对象
        proxyInvocationHandler.setRent(host);
 
        //创建代理对象
        Rent proxy = (Rent) proxyInvocationHandler.getProxy();
        proxy.rent();
 
    }

Summary
Whether it is a dynamic agency or a static agency, its essence is to make the business of the "landlord" become single, just responsible for renting the house, and some tedious things such as house inspection, "domestic baby", running errands, etc., are all handed over to the intermediary Up. The advantage of this is that the division of labor is realized, and when the business is expanded, it is convenient for centralized management

Guess you like

Origin blog.csdn.net/HeZhiYing_/article/details/109284813