StructuralverifiedVerified

Proxy

Provides a surrogate or placeholder for another object to control access, add lazy initialization, caching, logging, or access control.

Coming Soon

errorWhat Problem Does the Proxy Pattern Solve?

Sometimes direct access to an object is undesirable or impossible—the object may be expensive to create, live on a remote server, require access control enforcement, or need usage logging. Embedding these concerns directly into the object violates the Single Responsibility Principle.

check_circleHow the Proxy Pattern Works

Create a Proxy class implementing the same interface as the real subject. The proxy holds a reference to the real object and intercepts calls before forwarding them, performing pre- or post-processing such as lazy initialization, authentication checks, caching, or logging. The client cannot distinguish the proxy from the real subject.

Proxy Pattern Architecture

hourglass_empty

Rendering diagram...

Implementation by Language

lightbulb

Proxy Pattern in the Real World

A corporate receptionist acts as a proxy for the CEO. When someone wants to meet the CEO, the receptionist checks credentials, schedules the meeting, and logs the visit before granting access. The visitor interacts with the receptionist using the same protocol they would use with the CEO—the receptionist simply adds control and record-keeping around that access.

Frequently Asked Questions

helpWhat are the different types of Proxy?

The most common types are: Virtual Proxy (delays expensive object creation until needed), Protection Proxy (controls access based on permissions), Remote Proxy (represents an object in a different address space), and Caching Proxy (stores results of expensive operations).

helpWhat is the difference between Proxy and Decorator?

Both wrap an object and implement the same interface, but their intent differs. Proxy controls access to the object (lazy loading, access control, logging). Decorator adds new behavior or responsibilities. A Proxy typically manages the lifecycle of its subject; a Decorator does not.