[design pattern] - Proxy Pattern

Proxy Pattern

1 Introduction: Agency mode is also called entrustment mode, which is a structural design mode.

2 Definition: provide a proxy for other objects to control access to this object.

3 usage scenarios: (1) do not want to directly access an object; (2) when it is impossible or difficult to access an object, it can be accessed indirectly through a proxy object. In order to ensure the transparency of client use, the delegate object and proxy object need to implement the same interface.

Class 4 Diagram:

(1) Subject: abstract topic class.

The main responsibility of the abstract topic class is to declare the common interface methods of the real topic and the agent. This class can be either an abstract class or an interface.

(2) RealSubject: real topic class.

It can also be called delegate class or delegate class. It defines the real object, which executes the specific business logic methods. The customer class indirectly calls the methods defined in the real topic class.

(3) ProxySubject: proxy class

It can be called delegate class or proxy class. This class holds a reference of real topic class, and invokes the corresponding interface method in the real topic class in its implemented interface method, so as to achieve the role of the agent.

(4) Client: customer class, that is, the user of agent class.

5 code example

(1) Subject class

public interface Subject {
    public abstract void visit();


public class RealSubject implements Subject {
    public void visit() {



public class ProxySubject implements Subject{
    // Hold references to real topics
    private RealSubject mSubject;

    public ProxySubject(RealSubject mSubject) {
        this.mSubject = mSubject;

    public void visit() {

(4) Client class

public class Client {
    public static void main(String[] args) {
        RealSubject realSubject = new RealSubject();

        ProxySubject proxy = new ProxySubject(realSubject);



II. Static agent and dynamic agent

1 static agent: the code of the agent is generated by the programmer himself or through some automatic tools, and then compiled. That is to say, the class compilation file of the agent class already exists before the code runs.

2 dynamic agent: dynamically generate the agent's object through the reflection mechanism. In the coding stage, you don't need to know who to proxy, and who will decide in the execution stage. Java provides a dynamic proxy interface InvocationHandler. To implement this interface, you need to rewrite the invoke() method.

3 dynamic proxy code example:


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class DynamicProxy implements InvocationHandler {
    private Object obj;

    public DynamicProxy(Object obj) {
        this.obj = obj;

    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        Object result = method.invoke(obj, objects);
        return result;


import java.lang.reflect.Proxy;

public class DynamicClient {
    public static void main(String[] args) {
        RealSubject real = new RealSubject();

        DynamicProxy proxy = new DynamicProxy(real);

        ClassLoader loader = real.getClass().getClassLoader();

        ProxySubject subject = (ProxySubject) Proxy.newProxyInstance(loader, new Class[]{ProxySubject.class}, proxy);


4 advantages of static agent: dynamic agent can represent multiple proxy classes through one proxy class. In fact, it decouples the agent and the proxy, so that there is no direct coupling relationship between them.

Keywords: Design Pattern Dynamic Proxy

Added by DJ Digital Gem on Thu, 03 Mar 2022 23:55:44 +0200