The Proxy Design Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. A proxy class represents and controls access to the real object while adding additional behavior if necessary.
It a structural design pattern that provides a surrogate or placeholder object which controls access to another object.
It is commonly used when direct access to the object is expensive, unsafe, or needs control.
In simple words:
A proxy acts like a middleman between the client and the real object.
Real-life Analogy
1 Bank ATM
- You don't directly talk to the bank's main server.
- Instead, the ATM acts as a proxy.
- It checks your credentialys (PIN, card validity), and only if allowed, forwards your request to the bank system.
ATM = Proxy
Bank system = Real object
2 Security Guard at a Building
- You want to enter an office.
- The security guard (proxy) checks your ID card or pass.
- If valid, you can enter and interact with the office.
Guard = Proxy
Office building = Real object
Problem It Solves
Sometimes, direct access to an object is not desirable or practical.
Why? Because the object might be:
- Expensive the create (e.g., loading large files or connecting to a remote server).
- Sensitive (e.g., needs security checks before use).
- Remote (e.g., object lives on a different machine or server).
- Needs extra features (e.g., caching, logging, lazy initialization).
Without Proxy (Problem):
Imagine you have a large image file:
RealImage img("big_photo.png");
img.display();- Even if you never display it, the contructor loads it from disk.
- Wastes time & memory if you don't always need it.
- If access should be restricted (say only admins), there's no way to prevent unauthorized users from calling
display().
With Proxy (Solution):
You can create a ProxyImage that:
- Lazily loads the
RealImageonly whendisplay()is actually called. - Check access control (only authorized users can call
display()). - Caches the loaded image, so future calls don't reload it.
The client still just uses img.display(), but now access is controlled and optimized.
Types of Proxy
- Virtual Proxy: Controls access to an object that is resource-intensive to create (e.g., loading large images).
- Protection Proxy: Controls access based on permissions (e.g., user roles).
- Remote Proxy: Represents an object in another address space (like RPC).
1️⃣ Virtual Proxy Pattern
The Virtual Proxy controls access to a resource-intensive object by creating it only when needs. This helps improve performance and save memory.
Real-life Analogy
Think of a photo gallery app:
- When you open the gallery, it shows thumbnils first (fast, lightweight).
- The full-size image (big file) is only loaded when you actually click on it.
Thumbnails act like a virtual proxy for the real image.
Without Virtual Proxy
Here, the

Join the discussion
Sign in with your account to post comments, reply to others, and participate in the conversation.
Login to Comment