The Unprotected Resource Problem
In this design, the client calls the heavy service directly, with nothing in between. This creates a "choke point" (a place where everything gets stuck). Costly work is repeated for no reason, and nothing controls who can get data or how.
1. Real-Life Coding Example
Imagine you are building a video streaming app (think YouTube or Netflix) where users can download videos. Many users may ask for the same video. The same user may also ask again.
If the app downloads the video from the internet every single time, it causes:
- Unnecessary network calls: Repeatedly hitting the server for the same data.
- Longer wait times: Users wait for a full download even if the data was fetched a moment ago.
- Wasted bandwidth: This costs money for both the service provider and the user.
2. Key Issues in this Design
Lack of Caching (Redundant Execution)
- The Issue: Nothing stores videos that were already downloaded.
- The Impact: The same video is downloaded again and again, even if it is already available locally. This wastes computing work and network use.
Missing Access Control & Filtering
- The Issue: The
RealVideoDownloaderis called directly. Nothing checks the caller first. - The Impact: Any video URL is downloaded. There is no place to add content filtering, premium user checks, or security rules.
Tight Client Coupling
- The Issue: The client code uses the
RealVideoDownloaderclass directly. The Before code has noVideoDownloaderinterface. - The Impact: You cannot intercept, log, or change the download behavior (e.g., add "retry" logic) without editing the downloader's own code.
Inefficient Resource Management
- The Issue: Every request runs the full download again. The repo code creates one downloader and calls it twice, and both calls download.
- The Impact: This means repeated high cost, especially when the "Real Subject" (the real object behind the proxy) is expensive to set up.
3. Structural Analysis
| Component | Status | Observation |
|---|---|---|
| Real Subject | Exposed | Performs heavy network I/O for every single request. |
| Client | Tightly Coupled | Creates and calls the expensive resource itself. |
| Data Flow | Unoptimized | No middle layer to check whether the request needs to reach the network at all. |
4. Summary of the "Smell"
Watch for this: a client calls a costly or sensitive object directly, with no protective layer. The old code called RealVideoDownloader for every request, so it downloaded the same content again and again. The Proxy Pattern adds a "Placeholder" in front that manages these calls.