The Proxy Design Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. It's used when you want to add an additional layer of control over an object without modifying its code.
Here's a visual representation of the Proxy Design Pattern. It illustrates the relationship between the Client, Proxy, and RealSubject, all structured clearly to show how the Proxy acts as an intermediary.🧠 Core Idea:
Instead of accessing the actual object directly, you use a proxy object that performs additional operations (like access control, logging, lazy loading, etc.) before or after delegating the request to the real object.
🔧 Types of Proxy:
-
Virtual Proxy – Controls access to a resource that is expensive to create (e.g., lazily loading large objects).
-
Protection Proxy – Controls access by checking permissions (e.g., role-based access).
-
Remote Proxy – Represents an object that exists in a different address space (like over a network).
-
Smart Proxy – Adds extra functionality (e.g., reference counting, logging, caching) when accessing an object.
🧱 Structure:
-
Subject – Common interface for RealSubject and Proxy.
-
RealSubject – The actual object that does the work.
-
Proxy – Controls access to RealSubject.
🧑💻 Example in Pseudocode (Virtual Proxy):
Here's a simple Java implementation of the Proxy Design Pattern, using the classic Virtual Proxy example where an image is loaded only when needed.
🧱 Step-by-step Components:
1. Subject Interface
2. RealSubject (RealImage)
3. Proxy (ProxyImage)
4. Client Code
0 Comments