1. Definitions
The proxy pattern is a structural pattern that provides a proxy for other objects to control access to this object.
Every company has a boss, but not everyone can see the boss, and not everyone can let the boss do things. A secretary is mainly responsible for docking with the boss. If there is any meeting, contact the Secretary first, and then the Secretary will tell the boss. The Secretary can be understood as an agent.
2. Structure diagram of agent mode
Person is an abstraction of the agent and the agent, so that the agent and the agent are consistent. At the same time, person defines the interface for approving important documents and meeting with several department heads. The CEO and SeretaryProxy are implemented in person. SeretaryProxy is the Secretary and the agent of the CEO.

3. Implementation of agent mode
Person specifies the consistency between the agent and the agent, and defines two abstract classes: approving important documents and meeting with several important principals.
public interface Person { /** * Approve important documents */ void approve(); /** * attend a meeting */ void haveAMeeting(); }
The CEO is implemented in Person, and the implementation method is only simple output printing. The reason why the construction method is defined as protected is that the agent only wants to find him, and cannot contact the CEO through other ways, so the agent should be in the same package as the CEO, and the caller should call the agent.
public class CEO implements Person{ protected CEO() { } @Override public void approve() { System.out.println("Approve important documents..."); } @Override public void haveAMeeting() { System.out.println("Meeting with several department heads..."); } }
The SecretaryProxy is a secretary. In fact, you can choose to do some work if you can do it yourself, but you can't handle meetings and approving important documents by yourself, so you need the CEO to handle it. At the same time, you can do some processing before and after the proxy.
For example, if you are logging in, you can check whether the password is Chinese before the agent. Or whether it is null verification, and the actual login is processed through the agent.
public class SecretaryProxy implements Person{ private Person person; /** * Acting CEO in secretary class */ public SecretaryProxy() { person = new CEO(); } /** * Let the ceo approve the documents through the agent */ @Override public void approve() { System.out.println("Agent notification CEO Approval documents"); person.approve(); } /** * Let the ceo meet through proxy */ @Override public void haveAMeeting() { System.out.println("Agent notification CEO attend a meeting"); person.haveAMeeting(); } }
In actual use, it can be found that we only operate as an agent. When we need to approve documents and hold meetings, we are looking for an agent, not the CEO. As for whether the secretary needs to find the CEO, the caller doesn't need to care at all. If the agent can handle it by himself, he can handle it by himself. If not, he can leave it to the CEO.
In fact, the generation of the CEO in the instance code is generated when calling the construction method of the securityproxy, which is dead, but we can pass in a surrogate to solve this problem.
public class Test { public static void main(String[] args) { Person secretaryProxy = new SecretaryProxy(); secretaryProxy.approve(); secretaryProxy.haveAMeeting(); } }
Agent notification CEO Approval documents Approve important documents... Agent notification CEO attend a meeting Meeting with several department heads...
Roles in proxy mode
Subject, which specifies the consistency between the agent and the agent. In this article, the Person class plays this role.
Proxy (agent) handles things that can be handled by itself. Only when it can't be handled by itself will it find a real person to handle it. In this article, the SecretaryProxy class plays this role.
Realsubject (the actual Subject) implements the Subject and is equivalent to the agent. In this article, the CEO class plays this role.
Client (caller) is the role of accessing the proxy. In this paper, the Test class plays this role.
Differences among proxy mode, adapter mode and decorator mode
The proxy mode can do it by itself. When it cannot, it is handed over to the real person for processing, and the adapter prefers to reuse the original interface and use it without changing the original interface.
Decorator mode pays more attention to the enhancement of function. The agent pays more attention to entrustment, and the agent can only be accessed by the agent.
Reference: graphic design pattern
Code acquisition address: https://gitee.com/bughong/design-pattern