Initial knowledge of dynamic proxy mode

Posted by ShadowBlade72 on Fri, 10 Sep 2021 06:05:45 +0200

Initial knowledge of dynamic proxy

1 Agent mode

1.1 Purpose: The purpose of proxy mode is to provide a proxy for other objects to control access to this object. Use a proxy to wrap the object and replace the original object with the proxy object. Any call to the original object is made through the proxy. The proxy object decides whether and when to transfer method calls to the original object.
1.2 Main Solutions: Problems that arise when accessing objects directly, such as: objects to be accessed are on remote machines. In object-oriented systems, some objects are for some reason (e.g., object creation is expensive, some operations require security control, or out-of-process access), direct access can cause a lot of trouble to users or the system structure, we can add an access layer to this object when we access it.
1.3 When to use: You want to do some control when accessing a class.
1.4 Advantages: Clear responsibilities, high expansibility and intelligence.
1.5 Disadvantages: Due to the addition of proxy objects between the client and the real subject, some types of proxy modes may cause requests to be processed more slowly; implementing proxy modes requires additional work, and some proxy modes are very complex to implement.

2 Static Proxy

interface TicketOffice{
    void sellTicket();
}
// Proxy Class
class RailwayStation implements TicketOffice{

    @Override
    public void sellTicket() {
        System.out.println("The station really sold a ticket");
    }
}
// proxy class
class ProxyStation implements TicketOffice{
    private TicketOffice ticketOffice;
    public ProxyStation(TicketOffice ticketOffice){
        this.ticketOffice = ticketOffice;
    }
    
    @Override
    public void sellTicket() {
        System.out.println("Work before ticket sales");
        ticketOffice.sellTicket();
        System.out.println("Work after ticket sale");
    }
}
// Test Class
public class StaticProxyTest {
    public static void main(String[] args) {
        // Create an object of a proxy class
        TicketOffice rs = new RailwayStation();
        // Object to create proxy class
        TicketOffice ps = new ProxyStation(rs);

        ps.sellTicket();
    }
}

3 Dynamic Proxy

When a static proxy is executed, the classes of the proxy class and the target object are determined during compilation, which is not conducive to the expansion of the program. At the same time, each proxy class can only serve one interface, so that there will inevitably be too many proxies in the development of the program. It is best to complete all proxy functions through one proxy class.
Dynamic proxy that creates proxy classes at runtime using a reflection mechanism.

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

interface Hello {
    void sayHello();
}

// Proxy Class
class HelloImpl implements Hello{

    @Override
    public void sayHello() {
        System.out.println("Hello world!");
    }
}

class MyInvocationHandler2 implements InvocationHandler {
    private Object target; // Target object

    public MyInvocationHandler2(Object target){
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Pre-notification code");
        // Execute the appropriate target method
        Object rs = method.invoke(target, args);
        System.out.println("Post-processing code");
        return rs;
    }
}

public class ProxyTest2 {
    public static void main(String[] args) {
        HelloImpl helloImpl = new HelloImpl();
        Hello hello = (Hello) Proxy.newProxyInstance(Hello.class.getClassLoader(), new Class[]{Hello.class}, new MyInvocationHandler2(helloImpl));
        hello.sayHello();
    }
}

Topics: Java Design Pattern