Skip to content

The Surrogate for Controlled Access

A stand-in that controls access to the real object

Builds on Interfaces

Stage 3core3 min read

The Surrogate for Controlled Access

Structural design patterns are about how classes and objects fit together. They help manage the links between objects. The Proxy Pattern is one of them. A proxy is a stand-in for another object, and it decides how that object is reached.

The problem: sometimes calling an object directly is slow, costly, or unsafe. A proxy sits in front and handles the call first.


1. Proxy Pattern

The Proxy Pattern is a structural design pattern. It gives you a stand-in object that controls access to another object. The proxy sits in the middle and has the same interface as the real object (the same list of methods). The client cannot tell them apart, and the proxy can catch each request before it reaches the real object.

Real-Life Analogy: The Personal Assistant

Think of a busy CEO who does not answer everyone directly. An assistant (the proxy) takes calls and filters emails. The assistant brings in the CEO (the real resource) only when needed. Callers still get the help they need, and the CEO's time is protected.

Problem It Solves

It solves the problem of access to an object that is expensive or needs control.

  • Lazy Loading: Waiting to create a heavy object (like a video player) until it is really needed.
  • Remote Access: Handling the network calls to an object that lives on another server.
  • Governance: Adding logging, caching, or security without changing the original object.

2. Class Diagram

In the diagram below, RealVideoDownloader and the proxy CachedVideoDownloader both implement the VideoDownloader interface. The proxy holds a real downloader and a cache.

Loading diagram…

3. Real-World Coding Example: Video Streaming

Imagine a streaming app like YouTube or Netflix. Several users, or one user, may ask for the same video. If the app downloads it from the internet every time, it makes needless network calls, makes people wait longer, and wastes bandwidth.

Understanding the Issues (Without Proxy)

  • No Caching: The same video is downloaded again and again, even when the app already has it.
  • No Access Control: Any URL is downloaded. Nothing restricts or filters it.
  • Tight Coupling: The client uses RealVideoDownloader directly. To intercept or log a call, you would have to change the downloader's own code.
  • Redundant Resource Usage: The same costly download is run again and again, which wastes work.

How Proxy Pattern Solves the Issue

The proxy (CachedVideoDownloader class) has caching logic that checks whether a video was already downloaded.

  • If found, it returns the cached result, saving time and resources.
  • The real object is called only when needed, so responses are faster.

4. Types of Proxy

TypePurposeExample
Virtual ProxyControls access to a resource that is expensive to create. It creates the resource only when first needed (Lazy Initialization).A video app that only loads data when the user hits "Play".
Protection ProxyAllows or blocks access based on the user's permissions or role.A document editor where only "Editors" can modify content, but "Viewers" can only read.
Remote ProxyControls access to an object located on a remote server.An API wrapper that hides the network calls (e.g., Java RMI).
Smart ProxyAdds extra behavior like logging or reference counting (counting how many users hold the object).Automatically logging every time a sensitive file is accessed.

5. Pros and Cons

Advantages

  • Performance Optimization: Caching or lazy initialization cuts the resources used.
  • Access Control: Works as a gatekeeper, so only allowed users reach sensitive resources.
  • Added Functionality: Adds logging or security checks without changing the original object.

Disadvantages

  • Increased Complexity: There are more classes in the system, so the design is harder to maintain.
  • Potential Delays: Work done in the proxy, like permission checks or fetching data, can add small delays.
  • Maintenance Overhead: You have to maintain the proxies as well as the real objects, which takes more effort.

In this topic

Done with this topic?

How well do you know it?

PreviousDesign Pattern: Composite (Structural)