What if you could have a smart gatekeeper that protects your valuable resources without you lifting a finger?
Why Proxy pattern in LLD? - Purpose & Use Cases
Imagine you have a valuable painting in your home. You want to show it to guests but worry about damage or theft. So, you personally escort each guest to see the painting, watching closely every time.
This manual guarding is tiring and slow. You must be present all the time, which wastes your energy and time. If many guests come, you can't handle them all at once, and mistakes or accidents can happen.
The Proxy pattern acts like a trusted assistant who stands between guests and the painting. This assistant controls access, checks permissions, and even adds extra safety measures, so you don't have to watch constantly.
class RealSubject {
void request() { /* direct access */ }
}
// Client calls RealSubject directlyclass Proxy { RealSubject real; void request() { if (checkAccess()) { real.request(); } } bool checkAccess() { // access control logic return true; } } // Client calls Proxy instead of RealSubject
It enables controlled, safe, and efficient access to resources without exposing the real object directly.
In a video streaming service, a proxy can check if a user has paid before allowing access to the video, preventing unauthorized viewing.
Manual control of access is slow and risky.
Proxy acts as a protective middleman.
It improves security, performance, and flexibility.