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.
🟠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.
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.
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
- Composite Pattern — the last structural pattern (trees)
- Decorator Pattern — the look-alike with different intent
- All Design Patterns (hub)
- ← The complete System Design guide (hub)