Proxy Pattern
Proxy: start from the problem
See the responsibilities
A client should not directly load an expensive document until it needs the content. Another client may need an access check before reading. A representative object can mediate that interaction.
This is a structural pattern lesson. Follow the responsibilities, inspect the small JavaScript example and compare the design choices. The example is local teaching code; its boundary is explained before you apply the idea to a real service.
What you’ll learn
- Explain the problem Proxy addresses and the roles involved.
- Follow the example and distinguish this pattern from nearby alternatives.
- Choose when the benefit justifies the extra responsibilities and testing.
How the responsibilities work
Proxy presents a target-compatible interface while controlling access or indirection to the target. Common forms include lazy loading, local access checks and remote representatives. Decide what the client is allowed to assume about delay, errors and identity.
The lazy proxy below loads the document on its first read and reuses that local object. A remote proxy would additionally have network failures; it cannot honestly make a remote operation indistinguishable from a reliable local call.
Follow a small example
Read the JavaScript from top to bottom and compare the expected output. The values are chosen to make this pattern’s responsibility visible. This example runs in one local execution context; it does not create remote services, durable storage or production access controls.
JavaScript · local teaching example
let loads = 0;
const lazyDocument = (() => {
let target;
return {read() {
if (!target) { loads++; target = {read: () => 'Document text'}; }
return target.read();
}};
})();
console.log(lazyDocument.read());
console.log(lazyDocument.read());
console.log('loads=' + loads);Expected output
Document text
Document text
loads=1Choose a design and inspect the consequence
Compare the design choices
Compare the visible consequences. With JavaScript enabled, choose an option in each scenario to assemble your decision record.
This is an exploratory design exercise. It does not run infrastructure, measure capacity or replace the lesson’s quiz.
Apply the pattern to two situations. Select one option in each situation to build a decision record. Every option and its consequence stays visible, and you can change your choices.
When it helps—and what it costs
Use it for an intentional access or loading boundary. If the proxy is an authorization point, also enforce the correct rule at the resource boundary where bypass is possible.
Hidden I/O can surprise callers with latency or failure. Cached targets need invalidation and lifetime rules. A local wrapper does not authorize unrelated direct requests to a remote service.
Distinguish the neighbouring patterns
Decorator adds responsibilities around delegation. Adapter changes an interface. A network reverse proxy operates at a request/connection boundary, which is related in intent but has different transport responsibilities.
Follow wrapping additional behaviour, adapting the interface, network proxy responsibilities to compare the specific responsibility before combining patterns.
Check your pattern reasoning
Check your pattern reasoning
Name the responsibility before choosing the pattern
For Proxy, describe the problem, the participating roles and the observable contract. Use the example as a starting point and test the boundaries identified in the trade-offs. A pattern name alone does not establish correctness or performance.
Return to the design-pattern guide to compare families, or apply the idea alongside network proxy responsibilities.
Reaching the recap and completing the quizzes are separate milestones.