Java proxy pattern example 2017-01-04 02:36

You can use proxy pattern in your program, if you don't want use real object for some reason. For example, creating a real object is expensive or real object is in the remote machine. The key of proxy pattern is proxy object and real object implement the same interface and proxy object hold an instance of real object.

Proxy pattern

The code is like following.

public interface Calculator {
    int calculate(int a, int b);
}

public class RealObject implements Calculator {

    @Override
    public int calculate(int a, int b) {
        return a + b;
    }
}

public class Proxy implements Calculator {

    private RealObject realObject = null;

    public int calculate(int a, int b) {
        if (realObject == null) {//You can add your business logic here
            realObject = new RealObject();
        }
        return realObject.calculate(a, b);
    }
}

Client won't create real object directly.

public class Client {
    public static void main(String[] args) {
        Proxy proxy = new Proxy();
        System.out.println("result: " + proxy.calculate(1, 2));
    }
}