Proxy Pattern Explained: Code Samples and Real‑World Scenarios
This article introduces the Proxy design pattern, explains its core purpose of controlling access without modifying original classes, illustrates everyday analogies, provides a complete Java static‑proxy implementation with interface, real subject, proxy and test code, and summarizes common use cases and distinctions from decorators.
What is the Proxy Pattern?
The proxy pattern introduces an intermediate agent that clients interact with, while the proxy controls access to the real object and can add extra logic before or after the call.
The core goal is to isolate access and extend functionality without changing the original class code.
Real‑World Analogies
Real‑estate broker: clients never deal directly with the landlord; the broker shows houses, negotiates, and signs contracts.
Software scenarios: permission checks, request logging, caching, lazy loading, and RPC remote calls.
Image‑loading proxy: display a placeholder first, then load the high‑resolution image asynchronously.
Java Static‑Proxy Implementation (Rental Agency Example)
1. Unified Top‑Level Interface
// Rental action interface
public interface RentHouse {
void rent();
}2. Real Subject – Landlord
public class Landlord implements RentHouse {
@Override
public void rent() {
System.out.println("房东出租房屋");
}
}3. Proxy Object – Agency
public class AgencyProxy implements RentHouse {
private RentHouse target;
public AgencyProxy(RentHouse target) {
this.target = target;
}
@Override
public void rent() {
System.out.println("中介:带客户看房");
target.rent();
System.out.println("中介:收取中介费,签订合同");
}
}4. Test Invocation
public class Test {
public static void main(String[] args) {
RentHouse landlord = new Landlord();
RentHouse proxy = new AgencyProxy(landlord);
proxy.rent();
}
}Common Business Scenarios for Proxies
Remote proxy: RPC or remote interface calls that hide network details.
Protection proxy: permission validation, login interception, and access control.
Virtual proxy: lazy loading of large images or files, showing placeholders first.
Cache proxy: caching query results to reduce repeated database requests.
Logging/transaction proxy: Spring AOP’s core is dynamic proxying.
Key Knowledge Points
Static proxy: write a proxy class in advance; one target maps to one proxy.
Dynamic proxy (JDK/CGLIB): generate proxy classes at runtime, suitable for bulk enhancements.
Difference from the Decorator Pattern
Proxy focuses on controlling access permissions and isolating the object, whereas Decorator purely enhances the original functionality.
Conclusion and Preview
The twelfth episode on the proxy pattern is complete. All seven structural patterns have been covered, and the next series will start with the first behavioral pattern: the Chain of Responsibility.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
liandk
Seasoned Java and mobile developer with years of experience, specializing in mini‑programs, public accounts, and full‑stack front‑end development. In the AI era, I continuously learn to broaden my knowledge and evolve. I revived a public account I started a decade ago during a dessert‑startup venture, using code as a vessel and knowledge as a companion. I share personal projects, technical articles, programming tips, and growth insights—let’s improve together and set sail.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
