"Java" stop asking me what is a static proxy

Posted May 30, 20205 min read

In the past, an interviewer asked me the difference between a dynamic proxy and a static proxy. I did n’t make it clear at the time. I only mentioned that the dynamic proxy needs to implement the InvocationHandler interface, and then use the Proxy class reflection to create an instance cloud. As for static agents ... Isn't this a design idea?

The interviewer smiled, and people from all over the world never met.

I feel painful, I must understand the agent, so I have two articles on this and dynamic agents. I won't be afraid of the interviewer asking me about static proxy and dynamic proxy in the future!

1 What is an agent

When it comes to static proxies, one has to mention the proxy mode in design mode.

Baidu Encyclopedia defines the agency model as follows:

Provide a proxy for other objects to control access to this object. In some cases, one object is not suitable or cannot directly refer to another object, and the proxy object can act as an intermediary between the client and the target object.

There are two words in this definition:intermediary.

I think the following example can explain the proxy mode well.

I believe that programmers working hard in an unfamiliar city will encounter such a problem in the early stage:renting a house. We usually have three ways. Firstly, we can find listings on information websites such as Xianyu, Douban, Zirui and so on. Secondly, we can go directly to the community bulletin board to see if there is any lease information(of course it may be occupied by intermediary advertisements). The third is to contact a real estate agent, who will help you choose the house you want to rent, but only need to pay a service fee.

Rental picture from Baidu.png

Suppose I choose commission intermediary to rent a house. In this process, I can abstract the house as a class and the intermediary that helps me rent a house as a class. Both of these classes have the same method. And "I" as a client, commissioned an agent to help me find a house.

I entrust an agent to help me find a house. This process is an agent.

2 Static proxy

The so-called static proxy is that the .class file of the proxy class already exists before the program starts. The proxy class may be a .java file created directly by the programmer, or a .java file generated with the help of some tools, but without exception, it must be compiled into a .class file by the compiler before the program is started.

3 Definition and class diagram

Before writing the actual code as an example of a static proxy based on the rental example mentioned above, we must first understand a few roles in the proxy mode. There are three main roles in the proxy mode, namely abstract theme roles and real roles(being proxied). Role) and agent role.

3.1 Subject(Subject)

The theme role can be an interface or an abstract class. It defines the methods shared by the real role and the agent role, and the theme class makes the two have consistency.

At the same time, it is precisely based on the theme role to realize the function of the agent.

3.2 Real Character(RealSubject)

The real role is the delegated class, which is the house in the above example, and also the executor of the specific business logic.

3.3 Proxy Role(Proxy)

The inside of the agent role contains a reference to the real role RealSubject, which is responsible for the call to the agent role, and does pre-processing and post-processing before and after the agent role is processed.

In the above example of renting a house, it is a real estate agent.

3.4 Client

Someone may ask, what about "me"? Simply put, in fact, "I" is the main method in the test method, which is responsible for calling the method of the agent role.

3.5 Class diagram

The picture comes from Proxy mode-static proxy


4 Code example

Based on the above example of renting a house using code.

First create a theme role, which is an interface and has a method called intention, which is to be rewritten by the other two roles.

public interface Subject {

     * Common methods for each role
     * /
    void intention();

Then there is the real role, that is, the role of the agent, the real executor of business logic.

public class House implements Subject {

    public void intention() {
        System.out.println("I am the house the customer wants");

Then the agent class, which can enhance the method of the agent role. Agents are real estate agents who help me "find a good house".

A real estate agent only plays a role when he has a client(RealSubject). Before and after my "intention" is realized, it can enhance my "intention" respectively.

public class Proxy implements Subject {

    private RealSubject realSubject;

    public Proxy(RealSubject realSubject) {
        this.realSubject = realSubject;

    public void intention() {
        System.out.println("I am an intermediary, I will help customers find their favorite house");
        System.out.println("I am an intermediary, found");

Finally, "I" came out, "I" commissioned an agent to find a house.

public class Client {
    public static void main(String []args) {
        //I might like the house
        House house = new House();
        //Agents-real estate agency
        Proxy proxy = new Proxy(house);
        //"I" commissioned an agent to find a house

In the end, "I" executes the attention method of the agent class(intermediary). Since the agent class holds a real role(house), the program will also execute the attention method of the real role, which realizes the "I" commissioned intermediary to find the house. Static proxy process.

5 advantages and disadvantages of static proxy

5.1 Advantages

  1. The business class only needs to focus on the business logic itself, ensuring the reusability of the business class.
  2. The client only needs to know the proxy, and does not need to focus on the specific implementation.

The agent only needs to pay attention to the efficiency and quality of finding a house. Whoever wants to entrust an agent can find a house. "I" does not need to know how the agent finds a house, as long as he helps me find a house.

5.2 Disadvantages

  1. Because the proxy class and the proxy class both implement the theme interface, they all have the same method, resulting in a lot of code duplication. At the same time, if a method is added to the theme interface, then the proxy class and the proxy class also need to implement this method, increasing the complexity of maintaining the code.
  2. If the proxy class wants to provide delegation services for other real roles, it needs to implement other interfaces, and the code complexity will increase when the scale becomes larger.

If the intermediary not only provides rental services, but also provides a series of services such as playing games, selling houses, selling movie tickets, selling lottery tickets, chatting with games, playing games, etc., then he will become extremely complicated and no one will dare to move him He refers to the code).

6 Summary

This article describes the proxy model, static proxy, the role of the proxy model, the code implementation of the static proxy and the advantages and disadvantages of the static proxy.

Hope it helps everyone.

the above.