Proxy Design Pattern
The Proxy Design Pattern is one of the Structural Design Patterns. It provides a placeholder or surrogate object that controls access to another object, called the Real Subject. Instead of communicating directly with the real object, the client interacts with the proxy, which decides how and when requests should be forwarded.
A proxy can perform additional tasks before or after forwarding the request, such as checking permissions, creating the real object only when required (lazy initialization), caching results, logging activity, or controlling access to expensive resources.
The Proxy pattern is useful whenever direct access to an object needs to be managed without changing the object's implementation.
This document demonstrates two practical implementations of the Proxy Design Pattern in C++.
Example 1: Image Viewer with Lazy Loading
Loading high-resolution images from disk can be slow and consume significant memory. Instead of loading every image immediately, a proxy delays loading until the image is actually displayed.
Program
Explanation
The ImageProxy stores only the file name initially.
The RealImage object is created only when display() is called for the first time.
Subsequent calls reuse the already loaded image instead of loading it again, improving performance and reducing unnecessary resource usage.
Example 2: Secure Document Access
A company stores confidential documents that should only be accessible to authorized employees. The proxy checks user permissions before allowing access to the real document.
Program
Explanation
The client interacts only with SecureDocumentProxy.
The proxy verifies whether the user is authorized before creating or accessing the real document.
Unauthorized users are denied access, while authorized users transparently interact with the real object.
Types of Proxy
| Proxy Type | Purpose |
|---|---|
| Virtual Proxy | Creates expensive objects only when they are actually needed (lazy loading). |
| Protection Proxy | Controls access based on permissions or authentication. |
| Remote Proxy | Represents an object located on another machine or server. |
| Caching Proxy | Stores previously computed results to improve performance. |
| Logging Proxy | Records requests before forwarding them to the real object. |
Characteristics of the Proxy Design Pattern
| Property | Description |
|---|---|
| Pattern Type | Structural Design Pattern |
| Core Principle | Provide a surrogate object that controls access to another object. |
| Main Components | Subject, Real Subject, Proxy, and Client. |
| Relationship | The Proxy implements the same interface as the Real Subject and forwards requests after performing additional work. |
| Primary Benefit | Adds extra behavior such as lazy initialization, security, caching, or logging without modifying the real object. |
| Common Applications | Image viewers, database connections, remote services, authentication systems, API gateways, caching layers, and virtual memory systems. |
Conclusion
The Proxy Design Pattern introduces an intermediary object that manages access to another object while preserving the same interface. This enables features such as lazy loading, access control, logging, and caching without changing the underlying implementation. As demonstrated in the Image Viewer and Secure Document Access examples, the Proxy pattern improves flexibility, performance, and security while keeping client code simple and unaware of the additional processing performed behind the scenes.