Proxy
Put a stand-in with the same interface in front of an object to control access to it: check permissions, create it lazily, or reach it remotely.
Description
A proxy implements the same interface as the real object and holds (or knows how to get) a reference to it. Callers can't tell the difference. The proxy decides whether, when and how each call reaches the real object.
Common kinds: a protection proxy checks permissions; a virtual proxy delays creating something expensive until it's actually used; a remote proxy makes a network service look like a local object (RPC stubs); a caching proxy answers repeat requests itself. The example combines protection and lazy creation.
Class diagram
Proxy pattern class diagram
- Class DocumentsPage with + show(id).
- Interface DocumentStore with + read(id, user): string.
- Class SecureDocumentProxy with + read(id, user): string.
- Class S3DocumentStore with + read(id, user): string.
- DocumentsPage uses DocumentStore.
- SecureDocumentProxy implements DocumentStore.
- S3DocumentStore implements DocumentStore.
- SecureDocumentProxy creates lazily, delegates S3DocumentStore.
Sequence diagram
Proxy pattern sequence diagram
- DocumentsPage → SecureDocumentProxy: read(id, guest).
- DocumentsPage receives "Error: may not read" back from SecureDocumentProxy.
- DocumentsPage → SecureDocumentProxy: read(id, Kim).
- SecureDocumentProxy → S3DocumentStore: new S3DocumentStore().
- SecureDocumentProxy → S3DocumentStore: read(id, Kim).
- SecureDocumentProxy receives "contents" back from S3DocumentStore.
- DocumentsPage receives "contents" back from SecureDocumentProxy.
TypeScript example
A protection-and-lazy-loading proxy for a document store. Guests are refused at the door, and the expensive real store is only created on the first allowed read.
// Proxy: a stand-in for a document store that checks permissions and
// only creates the expensive real store when it's first needed.
interface User {
name: string;
role: "admin" | "member" | "guest";
}
// Subject
interface DocumentStore {
read(id: string, user: User): string;
}
// Real subject: expensive to create (connections, warm-up)
class S3DocumentStore implements DocumentStore {
static created = 0;
constructor() {
S3DocumentStore.created += 1; // pretend: open connections, load index
}
read(id: string, user: User): string {
return `contents of ${id} for ${user.name}`;
}
}
// Proxy: same interface, controls access and lifecycle
class SecureDocumentProxy implements DocumentStore {
private real?: DocumentStore;
private readonly createReal: () => DocumentStore;
constructor(createReal: () => DocumentStore) {
this.createReal = createReal;
}
read(id: string, user: User): string {
if (user.role === "guest") throw new Error(`${user.name} may not read ${id}`); // protection
this.real ??= this.createReal(); // lazy creation, on first allowed use
return this.real.read(id, user);
}
}
export function demo(): string[] {
const store: DocumentStore = new SecureDocumentProxy(() => new S3DocumentStore());
const lines = [`real store created: ${S3DocumentStore.created}`];
try {
store.read("contract-17.pdf", { name: "visitor", role: "guest" });
} catch (err) {
lines.push((err as Error).message);
}
lines.push(`real store created: ${S3DocumentStore.created}`);
lines.push(store.read("contract-17.pdf", { name: "Kim", role: "member" }));
lines.push(`real store created: ${S3DocumentStore.created}`);
return lines;
}When to use it
Use it when
- Access to an object must be checked or limited, and you don't want every caller to repeat the check.
- An object is expensive to create or load and often isn't needed at all.
- You want a remote or out-of-process service to look like a local object.
- You need to count, log or rate-limit calls to an object without changing it.
Avoid it when
- The object is cheap and access is unrestricted. A proxy adds a layer for nothing.
- You're adding features rather than controlling access. That's a Decorator, and naming it clearly helps readers.
- The remote call's latency and failure modes matter to callers. Hiding a network behind a local-looking interface can mislead.
Pitfalls
- Remote proxies that make network calls look free, which encourages chatty, slow code. Make remoteness visible in names or types.
- Lazy creation that isn't safe under concurrency, creating the real object twice. Guard it in multi-threaded or async code.
- Permission logic split between the proxy and the real object. Decide where authorization lives.
Alternatives
Decorator
Choose it when you add behaviour such as logging or caching that the caller composes, rather than control access.
Adapter
Choose it when the stand-in must offer a different interface from the real object.
Facade
Choose it when you want a simpler interface over several objects, not a stand-in for one.
JavaScript Proxy object
Choose it when you need to intercept arbitrary property access dynamically, for example in a reactive store; use with care.