About Us Contact Us Write for Us Advertise
Home > System Design > Low-Level Design > Proxy Pattern in Java (Virtual, Protection, Remote)
System Design › Low-Level Design

Proxy Pattern in Java (Virtual, Protection, Remote)

The Proxy pattern explained: a stand-in that controls access to a real object — virtual (lazy load), protection (access control), remote, and caching proxies. Proxy vs Decorator, Java code, and real examples.

Shiv Pandey
Shiv Pandey
Oct 21, 2026 | 81 views
Proxy Pattern in Java (Virtual, Protection, Remote)

🟠 Design Patterns · Structural · Senior

The Proxy pattern puts a stand-in object in front of a real one to control access to it. The proxy looks identical to the real object (same interface), so callers can't tell the difference — but it can add checks, delay loading, cache results, or represent something far away, all before (or instead of) passing the call through. It's a versatile pattern with several distinct flavours.

In one line: Proxy is a placeholder that implements the same interface as a real object and controls access to it — adding lazy loading, access control, caching, or remote communication without the client knowing.

The idea

🟠 A proxy wraps the real object behind the same interface. The client talks to the proxy; the proxy decides what to do — check a permission, create the real object lazily, return a cached value, or forward the call across a network — then (usually) delegates to the real thing.

Client Proxychecks / delays RealObjectdoes the work maybe same interface — client can't tell

The main types of proxy

🔴 Naming the flavours is what impresses in an interview — each solves a different problem:

Type What it does
Virtual proxy Delays creating an expensive object until it's actually needed (lazy loading) — e.g. don't load a huge image until it scrolls into view.
Protection proxy Checks permissions before allowing a call — access control in front of the real object.
Remote proxy Represents an object that lives on another machine, hiding the network call (e.g. an RPC stub).
Caching proxy Stores results of expensive calls and returns the cached value on repeat requests.
interface Image { void display(); }

class RealImage implements Image {                 // expensive
    RealImage(String file){ loadFromDisk(file); }  // heavy work in ctor
    public void display(){ /* render */ }
}
class ImageProxy implements Image {                // virtual proxy
    private final String file; private RealImage real;
    ImageProxy(String file){ this.file = file; }   // cheap — no load yet
    public void display() {
        if (real == null) real = new RealImage(file);  // lazy-load on first use
        real.display();
    }
}

Proxy vs Decorator (they look identical)

🟠 Both wrap an object with the same interface, so structurally they're twins — the difference is intent. Decorator adds new behaviour (features), and you stack many. Proxy controls access to the same behaviour (when/whether/who), usually one layer. Decorator enhances; Proxy guards or manages. Stating that intent distinction is exactly the senior nuance interviewers want.

Where you see it

Proxies are everywhere: Spring AOP wraps beans in proxies to add transactions/security transparently; Hibernate uses lazy-loading proxies for related entities; RPC frameworks generate remote-proxy stubs; API gateways act as protection/caching proxies. Recognising "that's a proxy" in these tools shows real understanding.

When to use it

🟠 Use a Proxy when you need to control access to an object without changing it: defer expensive creation (virtual), enforce permissions (protection), talk to a remote object (remote), or cache results (caching). If you want to add features rather than control access, that's Decorator instead.

The interview-ready summary

"Proxy is a stand-in that implements the same interface as a real object and controls access to it — the client can't tell the difference. The flavours: virtual (lazy-load an expensive object), protection (permission checks), remote (represent an object on another machine), and caching. It's structurally like Decorator but the intent is control, not adding behaviour. Real examples: Spring AOP proxies, Hibernate lazy loading, RPC stubs." That covers the intent, the types, and the Decorator contrast."

What to read next

← Facade pattern · Composite pattern →

Related Articles

Chain of Responsibility Pattern in Java (Example)
System Design › Low-Level Design

Chain of Responsibility Pattern in Java (Example)

Template Method Pattern in Java (with Example)
System Design › Low-Level Design

Template Method Pattern in Java (with Example)

Command Pattern in Java (with Undo Example)
System Design › Low-Level Design

Command Pattern in Java (with Undo Example)

State Pattern in Java (with Example)
System Design › Low-Level Design

State Pattern in Java (with Example)