What is Proxy Design Pattern?
The Proxy Pattern is a structural design pattern.
It provides a surrogate or placeholder object that controls access to another object (the real subject).
Think of it as a middleman that:
- Adds extra behavior (logging, security, caching, lazy loading, etc.)
- Controls expensive or sensitive object creation.
- Hides complexities from clients.
Real-World Analogy
Imagine you want to access a bank locker:
- You don’t directly access it.
- Instead, a proxy (bank staff) checks your credentials and gives you access.
- Here, the bank staff acts as the proxy to the locker (real subject).
Types of Proxy
- Virtual Proxy – Lazy load heavy objects.
- Protection Proxy – Control access with authentication/authorization.
- Remote Proxy – Act as local representative for remote objects (like Remote Method Invocation).
- Caching Proxy – Cache results for efficiency.
- Smart Proxy – Add extra actions (logging, monitoring, reference counting).
Structure (UML Breakdown)
The Proxy pattern has three main roles:
Subject (Interface/Abstract class): Common interface for both RealSubject and Proxy.
RealSubject: The actual object that does the real work.
Proxy: Acts as a placeholder, controls access to RealSubject.
Client → Subject → Proxy → RealSubject
Example implementation
Step 1: Create Subject Interface
public interface Image {
void display();
}
Step 2: Real Subject class
public class RealImage implements Image {
private String fileName;
public RealImage(String fileName) {
this.fileName = fileName;
loadFromDisk();
}
private void loadFromDisk() {
System.out.println("Loading " + fileName);
}
@Override
public void display() {
System.out.println("Displaying " + fileName);
}
}
Step 3: Proxy Class
public class ProxyImage implements Image {
private RealImage realImage;
private String fileName;
public ProxyImage(String fileName) {
this.fileName = fileName;
}
@Override
public void display() {
if (realImage == null) {
realImage = new RealImage(fileName); // Lazy loading
}
realImage.display();
}
}
Step 4: Client Code
public class ProxyPatternDemo {
public static void main(String[] args) {
Image image = new ProxyImage("photo.jpg");
// Image will be loaded from disk only once
image.display(); // Loading + Display
image.display(); // Only Display (no loading)
}
}
Output
Loading photo.jpg
Displaying photo.jpg
Displaying photo.jpg
Real-World Examples of Proxy in Java
- java.lang.reflect.Proxy → Dynamic proxies for implementing interfaces at runtime.
- Spring AOP Proxies → Used for method interception (logging, transactions, security).
- Hibernate Lazy Loading → Entities are represented using proxies until accessed.
Advantages of Proxy Pattern
✅ Provides controlled access to objects.
✅ Supports lazy initialization for performance.
✅ Adds extra functionality without modifying the real object.
Conclusion
The Proxy Design Pattern is a powerful structural pattern that allows us to control access, improve performance, and add extra features without touching the real object’s code.
It’s widely used in Java frameworks like Spring and Hibernate, making it a must-know pattern for every developer.
This is Part 6 of the Java Design Patterns Series.
If you find it insightful, please share your feedback. Also let me know if you have used proxy pattern in your projects.
Next Up: Facade Design Patterns!
Top comments (0)