Design Pattern

Proxy Pattern

Clean Java-only production-ready implementation.


What Problem It Solves

You need to control access to an object. The proxy acts as a surrogate, doing something before/after delegating to the real object โ€” lazy loading, access control, logging, caching, or remote communication.

Proxy vs Decorator

AspectProxyDecorator
PurposeControl access, manage lifecycleAdd behavior
Creates real object?Usually (virtual proxy)No (receives it)
InterfaceSame as real subjectSame as wrapped component
TransparencyClient may or may not know it's a proxyClient usually doesn't know

Proxy Types

TypeWhat It DoesLLD Use
Virtual ProxyLazy initialization โ€” defers expensive object creationLarge images, DB connections
Protection ProxyControls access based on permissionsAuth/Access Control
Logging ProxyLogs every call to the real objectDebugging, auditing
Caching ProxyCaches results of expensive operationsCache aside pattern
Remote ProxyRepresents an object in a different address spaceRPC, gRPC stubs

Implementation

// โ”€โ”€โ”€ EXAMPLE 1 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
// WHAT WE ARE IMPLEMENTING:
// A network video-loading gateway implementing an internal cache wrapper
// around a slow cloud storage SDK.
//
// WHERE THE PROXY FITS IN:
// VideoService is the Subject. RealVideoService is the Real Subject.
// ProxyVideoService is the Cache Proxy controlling access and caching
// results.
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
// --- Subject ---
interface Image {
    void display();
}

// --- Real subject ---
class RealImage implements Image {
    private final String filename;

    public RealImage(String filename) {
        this.filename = filename;
        loadFromDisk();  // Expensive operation
    }

    private void loadFromDisk() {
        System.out.println("  [RealImage] Loading " + filename + " from disk...");
        try { Thread.sleep(500); } catch (InterruptedException ignored) {}
    }

    public void display() {
        System.out.println("  [RealImage] Displaying " + filename);
    }
}

// --- Proxy (virtual proxy โ€” lazy load) ---
class ProxyImage implements Image {
    private final String filename;
    private RealImage realImage;

    public ProxyImage(String filename) { this.filename = filename; }

    public void display() {
        if (realImage == null) {
            realImage = new RealImage(filename);  // Lazy initialization
        }
        realImage.display();
    }
}

// --- Protection proxy ---
class ProtectedImageProxy implements Image {
    private final ProxyImage proxyImage;
    private final String userRole;

    public ProtectedImageProxy(String filename, String userRole) {
        this.proxyImage = new ProxyImage(filename);
        this.userRole = userRole;
    }

    public void display() {
        if (!userRole.equals("ADMIN")) {
            System.out.println("  [Proxy] Access denied: " + userRole + " cannot view " + proxyImage);
            return;
        }
        proxyImage.display();
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("--- Virtual Proxy (lazy load) ---");
        Image img = new ProxyImage("photo.jpg");
        System.out.println("Image created โ€” no loading yet");
        img.display();  // Loads only now

        System.out.println("\n--- Protection Proxy ---");
        Image protectedImg = new ProtectedImageProxy("confidential.png", "USER");
        protectedImg.display();  // Denied

        Image adminImg = new ProtectedImageProxy("confidential.png", "ADMIN");
        adminImg.display();  // Allowed
    }
}

๐Ÿ’ฌReview

Help Us Improve

How helpful was this walkthrough?

Click a star to rate. We actively use this feedback to refine and update our system design content.

Placeholder
Optional but highly appreciated!

Discussion

Share your thoughts, ask questions, or help others.

Loading comments...