Structural Design Pattern
Proxy Design Pattern in Java
Control access to another object for lazy loading, security, caching, or remote calls.
In this lesson: Proxy provides a substitute with the same interface as a real object. It can delay creation, check permissions, cache results, or forward calls to a remote service.
Overview
Proxy is useful when its recurring design problem appears in a real application. It keeps the relevant responsibilities organized while allowing surrounding code to depend on clear abstractions.
Implementation
interface Image { void display(); }
final class RealImage implements Image { RealImage(String path) { System.out.println("Loading " + path); } public void display() { System.out.println("Display image"); } }
final class ImageProxy implements Image { private final String path; private RealImage real; ImageProxy(String path) { this.path = path; } public void display() { if (real == null) real = new RealImage(path); real.display(); } }
Image image = new ImageProxy("photo.png");
image.display();
When should you use it?
Use Proxy when access to an object must be controlled or the real object is expensive to create or reach.
Next step: Continue with the Design Patterns course and explore the next pattern.