How do you implement the Proxy pattern in Java?

The Proxy pattern implements a stand-in object that shares the same interface as a real object and controls access to it, adding behavior like lazy initialization, access control, or logging before delegating to the real object.

Key Points

Example

A caching proxy for a slow ImageLoader could implement the same Image interface, check an internal cache first, and only delegate to the real loader on a cache miss, transparently speeding up repeated requests.

interface Image {
    void display();
}

class RealImage implements Image {
    private final String fileName;
    RealImage(String fileName) {
        this.fileName = fileName;
        loadFromDisk();
    }
    private void loadFromDisk() { /* expensive load */ }
    public void display() { System.out.println("Displaying " + fileName); }
}

class ProxyImage implements Image {
    private final String fileName;
    private RealImage realImage;

    ProxyImage(String fileName) {
        this.fileName = fileName;
    }

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

Interview Tip

A concise interview answer is

I implement Proxy by defining a common interface for the real object and the proxy, having the proxy hold a reference to the real object, and adding cross-cutting logic like lazy loading, caching, or access checks in the proxy before delegating the actual call to the real subject.

Last updated: 16 July 2026

Previous question: Can you explain the difference between a virtual proxy, remote proxy, and protection proxy?

Next question: When would you use the Proxy pattern in real-world applications?

More Java Design Patterns interview questions

All Java Design Patterns questions