WorkspaceLow-Level Design
← Course overview

Course navigation

On this page

Low-Level Design / Structural Patterns

pattern · 12 MIN READ

Proxy

A proxy controls access to a real object while preserving its interface. It can authorize, defer creation, or cache access, but it must not silently weaken the original contract.

Reading completion is independent of quizzes and challenges.

What you’ll understand

  • A proxy controls access to a real object while preserving its interface. It can authorize, defer creation, or cache access, but it must not silently weaken the original contract.
  • DocumentProxy checks the caller and delegates to the real document only on an allowed read.
  • Evaluate the tradeoff: Clients may encounter extra latency or authorization failures. Document these effects and avoid treating a proxy as a replacement for validation inside a security boundary.

Useful first: Classes and Objects

The idea and when to recognize it

A proxy controls access to a real object while preserving its interface. It can authorize, defer creation, or cache access, but it must not silently weaken the original contract.

  • Requirement: A document service must count real reads and deny access to callers without permission.
  • Change to anticipate: Return a denial without invoking the protected document when authorization fails.
  • Design goal: DocumentProxy checks the caller and delegates to the real document only on an allowed read.

Start with the concrete problem

A document service must count real reads and deny access to callers without permission.

Return a denial without invoking the protected document when authorization fails.

Direct implementationRevised design
Checking permission after reading the document exposes work or data that should never have been accessed.DocumentProxy checks the caller and delegates to the real document only on an allowed read.
The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design.Clients may encounter extra latency or authorization failures. Document these effects and avoid treating a proxy as a replacement for validation inside a security boundary.

Responsibilities and contracts

DocumentProxy checks the caller and delegates to the real document only on an allowed read.

  • DocumentProxy: Controls access to the real object. Public operations: read(allowed)
  • Document: Owns protected content. Public operations: read(), reads

Read the design

Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.

Preparing the design walkthrough…

Follow the interaction

Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.

Preparing the design walkthrough…

Implementation and expected output

Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.

Preparing the design walkthrough…

Try the idea in another setting

A lazy image proxy can defer loading a large image until the client first asks to display it.

  • Name the new client contract before adapting the example.
  • Identify which state belongs to each object and who controls its lifetime.
  • Write one normal case and one boundary case before extending the design.

Tradeoffs and interview takeaways

Clients may encounter extra latency or authorization failures. Document these effects and avoid treating a proxy as a replacement for validation inside a security boundary.

Common mistake: Checking permission after reading the document exposes work or data that should never have been accessed.

  • Explain the choice: DocumentProxy checks the caller and delegates to the real document only on an allowed read.
  • Challenge your choice: Clients may encounter extra latency or authorization failures. Document these effects and avoid treating a proxy as a replacement for validation inside a security boundary.
  • Describe a requirement that would make the simpler implementation sufficient.

Design challenge and knowledge check

Preparing the design walkthrough…

Make it your own

Saved on this device · 0/20,000

Reading completion is independent of quizzes and challenges.