Design pattern -- proxy pattern

brief introduction

Provide a proxy object for other objects, and the proxy object controls the access of this object.
characteristic
1) Very directly, implement the same interface or inherit the same abstract class.
2) The proxy object controls access to the proxy object.

The class diagram is as follows:

Roles in proxy mode:
Abstract Subject: it declares the common interface between the target object and the proxy object, so that the proxy object can be used wherever the target object can be used.
Realsubject: also known as the delegate role or the delegated role. Defines the target object represented by the proxy object.
Proxy subject role (Proxy): also called delegate class and proxy class. The proxy object contains the reference of the target object, so that the target object can be operated at any time; The proxy object provides the same interface as the target object so that the target object can be replaced at any time. Proxy objects usually perform an operation before or after the client call is passed to the target object, rather than simply passing the call to the target object.

Simple example static proxy

1. create an abstract Subject role (Subject)

public interface Printer {
    void print();
}

2. specific subject role (RealSubject)

public class ConsolePrinter implements Printer {
    private String name;

    public ConsolePrinter(String name) {
        this.name = name;
    }

    @Override
    public void print() {
        System.out.println("print : " + name);
    }
}

3. proxy subject role (Proxy)

public class ProxyPrinter implements Printer {
    private ConsolePrinter printer;
    private String name;

    public ProxyPrinter(String name) {
        this.name = name;
    }
    @Override
    public void print() {
        if (printer == null) {
            printer = new ConsolePrinter(name);
        }
        printer.print();
    }
}

4.Main verification

public class ProxyMain {
    public static void main(String[] args) {
        Printer printer = new ProxyPrinter("test");
        printer.print();
    }
}

Simple example of dynamic proxy

1.2.3 Ibid

Create a new printerinvocationhandler to implement the invocationhandler class.

public class PrinterInvocationHander implements InvocationHandler {
    // Declaration of the object of the proxied class that implements the interface
    //1. instantiate the proxy object;
    Object obj;
    // 2. return an object of proxy class
    public Object blind(Object obj) {
        this.obj = obj;
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
    }
    //When a method call to be overridden is initiated through a proxy class object, it will be converted to a call to the following invoke method
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object returnVal = method.invoke(obj, args);
        return returnVal;
    }
}

Main verify:

public class Test {
    public static void main(String[] args) {
        //1. object of proxy class
        ProxyPrinter printer = new ProxyPrinter("test");
        //2. create an object that implements the InvocationHandler interface class
        PrinterInvocationHander invocationHander = new PrinterInvocationHander();
        //3. call the blind() method to dynamically return an object that also implements the proxy class of the Printer interface implemented by the ProxyPrinter class
        Object obj = invocationHander.blind(printer);
        Printer printer1 = (Printer) obj;
        printer1.print();
    }
}

Tags: Java Design Pattern

Posted by moneytree on Mon, 30 May 2022 05:34:30 +0530