define

The proxy pattern simply means providing a proxy for other objects to control access to that object.

Usage scenarios

When you cannot or do not want to directly access an object or it is difficult to access an object, you can access it indirectly through a proxy object. To ensure transparency of client use, the proxy object and the proxy object need to implement the same interface

Static agent

UML class diagrams

Code implementation

Abstract Topic class

/*** * Abstract Topic class * The main responsibility of this class is to declare the common interface methods of the real topic and the proxy. This class can be either an abstract class or an interface */
public abstract class Subject {
    // A normal business method
    public abstract void visit(a);
}
Copy the code

A real topic class that implements an abstract topic

/*** * A real topic class that implements an abstract topic * this class is a delegated or proided class that defines a real object identified by the proxy to execute concrete business logic methods * while the client class indirectly calls the methods defined in the real topic class */ through the proxy class
public class RealSubject extends Subject {
    @Override
    public void visit(a) {
        System.out.println("Real subject visit"); }}Copy the code

The proxy class

/*** * proxy class * This class is a delegate or proxy class that holds a reference to a real topic class and acts as a proxy by calling the corresponding interface method in the topic class * in the interface method it implements
public class ProxySubject extends Subject{
    private RealSubject realSubject;

    public ProxySubject(RealSubject realSubject) {
        this.realSubject = realSubject;
    }

    @Override
    public void visit(a) {
        realSubject.visit(); // The proxy class invokes its methods via a reference to the real topic object}}Copy the code

Specific customer class

/*** * Specific customer */
public class Client {
    public static void main(String[] args) {
        RealSubject realSubject = new RealSubject();
        ProxySubject proxySubject = newProxySubject(realSubject); proxySubject.visit(); }}Copy the code

You can see in the image that the output is from our real proxy class.

A dynamic proxy

Code implementation

Dynamic proxy class interface definition

/*** * Dynamic proxy interface */
public interface Dynamic {
    void visit(a);
}

Copy the code

Proxy class concrete implementation

/*** * Dynamic proxy class implementation */
public class DynamicSubject  implements Dynamic{

    @Override
    public void visit(a) {
        System.out.println("Dynamic proxy output DynamicSubject"); }}Copy the code

Let’s take a look at the proxy class code

// The invoke method is used primarily to invoke a method of a specific propped class
public class DynamicProxy implements InvocationHandler {
    private Object object;  // Referenced by the proxied object

    public DynamicProxy(Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] args) throws Throwable {
        // Invoke the proxied object's method
       Object result = method.invoke(object, args);
        returnresult; }}Copy the code

Next, take a look at the client class

/*** * Specific customer */
public class Client {
    public static void main(String[] args) {
        DynamicSubject dynamicSubject = new DynamicSubject();
        // Since the proxy class holds a reference to Object, we can create multiple proxy objects for business purposes.
        DynamicProxy dynamicProxy = new DynamicProxy(dynamicSubject);
        // Get the ClassLoader of the proxied class
        ClassLoader loader = dynamicSubject.getClass().getClassLoader();
        Dynamic dynamic = (Dynamic) Proxy.newProxyInstance(loader,newClass[]{Dynamic.class},dynamicProxy); dynamic.visit(); }}Copy the code

Output after running:

Dynamic proxies output DynamicSubject

conclusion

  • The proxy mode is mainly divided into two types, one is static proxy, the other is dynamic proxy:

  • A static proxy class compilation file for the proxy class already exists before running the code.

  • Dynamic proxies generate proxy objects dynamically through reflection.

Dynamic proxy is often used in practical development.

advantages

  • Clear responsibilities, the real role is to achieve the actual business logic, do not care about other non-responsibility transactions, through the late agent to complete a completed transaction, the collateral result is simple and clear programming.
  • A proxy object can act as an intermediary between the client and the target object, thus acting as a mediator and protecting the target object.
  • High scalability

disadvantages

  • The amount of class code increases