Difference between Dynamic Proxy and Static Proxy

In Java, there are two types of proxies: dynamic proxy and static proxy. The main difference between them is that dynamic proxy is created at runtime, while static proxy is created at compile time.

Static Proxy

Static proxy is a design pattern where a proxy class is created to wrap around the real object. The proxy class has the same interface as the real object, so clients can use it in the same way. The proxy class then delegates the method calls to the real object, but can also perform additional tasks before or after the method call.

Here is an example of a static proxy:

interface Image {
    void display();
}

class RealImage implements Image {
    private String fileName;

    public RealImage(String fileName) {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }

    @Override
    public void display() {
        System.out.println("Displaying " + fileName);
    }

    private void loadFromDisk(String fileName) {
        System.out.println("Loading " + fileName);
    }
}

class ImageProxy implements Image {
    private RealImage realImage;
    private String fileName;

    public ImageProxy(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void display() {
        if (realImage == null) {
            realImage = new RealImage(fileName);
        }
        realImage.display();
    }
}

public class Main {
    public static void main(String[] args) {
        Image image = new ImageProxy("test.jpg");
        image.display();
    }
}

In this example, RealImage is the real object and ImageProxy is the proxy class. When ImageProxy.display() is called, it first checks if realImage is null. If it is, it creates a new RealImage object and assigns it to realImage. It then calls realImage.display() to display the image.

Dynamic Proxy

Dynamic proxy is a mechanism that allows you to create a proxy object at runtime without having to create a separate proxy class. The proxy object is created based on an interface or a set of interfaces, and all method calls to the proxy object are forwarded to an invocation handler.

Here is an example of a dynamic proxy:

interface Calculator {
    int add(int a, int b);
}

class CalculatorImpl implements Calculator {
    @Override
    public int add(int a, int b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calculator = (Calculator) Proxy.newProxyInstance(
                Calculator.class.getClassLoader(),
                new Class[] { Calculator.class },
                new InvocationHandler() {
                    private final Calculator calculator = new CalculatorImpl();

                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("Before method " + method.getName());
                        Object result = method.invoke(calculator, args);
                        System.out.println("After method " + method.getName());
                        return result;
                    }
                });
        int result = calculator.add(1, 2);
        System.out.println("Result: " + result);
    }
}

Proxy.newProxyInstance is a method in Java’s java.lang.reflect package that allows you to create a dynamic proxy object at runtime. The dynamic proxy object implements one or more interfaces and forwards all method calls to an invocation handler.

In this example, CalculatorImpl is the real object and Calculator is the interface. Proxy.newProxyInstance() creates a dynamic proxy object that implements the Calculator interface. The InvocationHandler is a callback interface that is called for each method call on the proxy object. In this example, the InvocationHandler logs the method name before and after the method call, and then delegates the method call to the real object.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值