Proxy Pattern
The Proxy Pattern is a structural design pattern that provides a surrogate or placeholder object which controls access to another object, allowing behavior such as lazy loading, access control, caching, or logging to be added transparently…
Definition
The Proxy Pattern is a structural design pattern that provides a surrogate or placeholder object which controls access to another object, allowing behavior such as lazy loading, access control, caching, or logging to be added transparently without changing the real object's interface.
Overview
The Proxy Pattern introduces an intermediary object — the proxy — that implements the same interface as a real subject object and forwards calls to it, while adding its own logic before or after the forwarding step. Because the proxy and the real object share an identical interface, client code can use a proxy interchangeably with the real object without any awareness that indirection is happening. The Gang of Four catalog identifies several common proxy variants: a virtual proxy defers creation of an expensive object until it's actually needed (lazy initialization); a protection proxy checks access permissions before allowing a call through; a remote proxy represents an object living in a different address space or over a network, hiding the complexity of remote communication; and a caching/smart-reference proxy adds caching, reference counting, or logging around calls to the real object. Proxies are pervasive in real-world software: ORMs use virtual proxies to lazily load related database records only when accessed; RPC and gRPC client stubs are remote proxies that make a network call look like a local method call; API gateways and reverse proxies like Nginx act as protection/caching proxies in front of backend services; and many mocking/testing frameworks generate dynamic proxy objects to intercept and verify method calls. JavaScript's built-in `Proxy` object is a direct, first-class language implementation of this pattern, letting developers intercept property access, assignment, and function calls on any target object. The Proxy Pattern is closely related to, but distinct from, Decorator: both wrap another object behind the same interface, but Proxy is primarily about controlling access to the wrapped object (deferring, restricting, or redirecting calls), while Decorator is about adding new behavior/responsibilities to the wrapped object without controlling access to it. Understanding this distinction helps clarify why frameworks that add authentication checks, lazy loading, or remote-call transparency are described as proxies, while frameworks that layer optional features (like middleware chains) are more often described as decorators.
Key Concepts
- Implements the same interface as the real subject it wraps
- Virtual proxy: defers expensive object creation until first use (lazy loading)
- Protection proxy: enforces access control before forwarding calls
- Remote proxy: represents an object in a different process or address space
- Caching/smart-reference proxy: adds caching, logging, or reference tracking
- Client code is unaware whether it's talking to the proxy or the real object
- Underlies ORMs' lazy loading and RPC/gRPC client stubs
- Native language support in JavaScript's built-in Proxy object