Proxy Pattern
Provide a surrogate or placeholder for another object to control access to it.
Intent & Objective
Control access to the original object, allowing you to perform something either before or after the request gets through to the original object (such as lazy initialization, caching, access control, or logging).
"Instead of meeting the CEO directly, you consult their executive assistant. The assistant controls access, filters inquiries, and schedules time with the real person."
"Checks identification and dress code at the building entrance before granting permission to enter the inner vault or club."
"Represents the client in court, presenting arguments and signing legal filings on their behalf without requiring the client to attend every hearing."
"A team lead acts as a proxy gatekeeper, verifying automated tests and compliance before allowing code to deploy to the production environment."
The Problem
Directly communicating with a resource-heavy object (like loading a 10GB 3D model or an unauthenticated remote service) freezes application startup or exposes sensitive APIs.
The Solution
Create a proxy class with the identical interface as the service. When client requests arrive, the proxy checks cache/permissions and lazily initializes the real service only when necessary.
Key Participants & Architectural Roles
Code Samples (Traditional OOP vs Functional Programming)
Available in C++, Python, and JavaParadigm Contrast: Traditional OOP vs Functional / Data-Oriented
In OOP, Proxy implements the target interface and delegates to a wrapped reference. In Functional Programming, Proxying is achieved through memoization, middleware wrappers, or function interceptors.
Advantages
- ✓ Allows controlling the service object without clients knowing about it
- ✓ Manages the lifecycle of heavy service objects when clients don’t care
- ✓ Works even when the service object isn’t ready or available yet (lazy loading)
Trade-offs
- ✗ Response might be slightly delayed by proxy processing layers
When to Apply
- → Virtual Proxy (Lazy loading heavy objects only when used)
- → Protection Proxy (Verifying user permissions before delegating to real service)
- → Caching Proxy (Memoizing results of expensive operations)
When to Avoid
- ! When direct access to the service has zero overhead and requires no mediation