Proxy mode--static proxy

Before clarifying AOP, you must first conduct an in-depth study of the proxy model.

The proxy mode is divided into static proxy and dynamic proxy. Dynamic agents include JDK agents and Cglib

This article mainly studies static agents.

proxy mode

Starting from life, I am a person who wants to rent a house. I want to rent a house and find a housing agency. There are many houses. I won’t go looking for landlords because they are hard to find and landlords have limited space. As for the landlord, I just want to rent out my house. I don’t want to find tenants, nor do I want to show tenants the house or do other things. For me and the landlord, we need an intermediary. This agent rented the house to me on behalf of the landlord. An agent is the landlord's agent. The agent and the landlord have a common activity in renting a house, leaving it to the agent to complete the final task.

Proxy modes include

  • Abstract role: Some behaviors of the role are usually implemented using abstract classes and interfaces.
  • Real role: the person being represented (landlord)
  • Agent role: acts as an agent for the real character and supplements the behavior of the real character (intermediary)
  • Client: The person who has access to the agent (me, the tenant)

The benefits of proxy mode:

  • It can make the operation of real characters more pure, without having to pay attention to public business
  • Public business is handed over to the agent role, realizing the division of labor in the business
  • When public services expand, centralized management is facilitated
  • You can add some other operations without changing the original code.

Hand-drawing the code of static proxy

abstract role

package com.heaboy.proxyExcise.staticExcise;

/**
 * @Author:XK
 * @Date: Created in 11:20 2022/2/19
 * @Description:静态代理的抽象行为
 **/
public interface Rent {
    public void rent();
}

real role

package com.heaboy.proxyExcise.staticExcise;

/**
 * @Author:XK
 * @Date: Created in 11:23 2022/2/19
 * @Description:静态代理的真实角色,也就是被代理的对象,租房模型中的房东
 **/
public class Host implements Rent {

    @Override
    public void rent() {
        System.out.println("房东出租房子");
    }
}

agent role

package com.heaboy.proxyExcise.staticExcise;

/**
 * @Author:XK
 * @Date: Created in 11:24 2022/2/19
 * @Description:代理对象,代理真实角色,租房模型的中介所
 **/
public class StaticProxy {
    private Host host;

    public StaticProxy() {
    };

    public StaticProxy(Host host) {
        this.host = host;
    }
    //代理房东的出租房子行为,并且可以加一下附加操作
    public void rent(){
        System.out.println("我是中介,出租我代理房东的房子");
        host.rent();
    }
    //甚至可以追加一些其他的行为
    public void kanfang(){
        System.out.println("我是中介,带客人看我代理房东的房子");
    }
}

It not only realizes the behavior of the landlord, but also enhances the behavior of the landlord, and has additional behaviors to check the house. 

client

package com.heaboy.proxyExcise.staticExcise;

/**
 * @Author:XK
 * @Date: Created in 11:27 2022/2/19
 * @Description:客户端获取到代理对象,出租模型中代表房客
 **/
public class Client {
    public static void main(String[] args) {
        StaticProxy staticProxy = new StaticProxy(new Host());
        staticProxy.kanfang();
        staticProxy.rent();
    }
}

operation result:

I am an agent. I show clients the house of my landlord.
I am an agent. I rent the house of my landlord.
The landlord rents the house.

Process finished with exit code 0

 If you go to the landlord directly without using an agent:

Guess you like

Origin blog.csdn.net/Yoke______/article/details/123016221