Proxy Design Pattern
What is the Proxy Design Pattern?
The Proxy Design Pattern is a structural design pattern that provides a substitute or placeholder object for another real object. Instead of directly accessing the real object, the client interacts with the proxy. The proxy then controls access to the real object.
In simple words, a proxy acts like a middleman between the client and the actual object. It can add extra logic such as security checks, logging, or lazy loading before passing the request to the real object.
Why Do We Need a Proxy?
Sometimes, creating or accessing an object is expensive or sensitive. The Proxy pattern helps in such situations by:
- Controlling access to the object
- Improving performance using lazy initialization
- Adding security or permission checks
- Logging or monitoring method calls
Real-Life Example
Think of an ATM card. You do not directly access the bank server. Instead, you use the ATM card (proxy), which verifies your PIN and then allows access to your bank account.
Types of Proxy
- Virtual Proxy – Creates objects only when needed
- Protection Proxy – Controls access based on permissions
- Remote Proxy – Represents an object located remotely
Proxy Design Pattern – Java Example
Step 1: Create an Interface
This interface will be implemented by both the real object and the proxy.
public interface Image {
void display();
}
Step 2: Create the Real Object
This is the actual object that performs the main operation.
public class RealImage implements Image {
private String fileName;
public RealImage(String fileName) {
this.fileName = fileName;
loadImageFromDisk();
}
private void loadImageFromDisk() {
System.out.println("Loading image: " + fileName);
}
@Override
public void display() {
System.out.println("Displaying image: " + fileName);
}
}
Step 3: Create the Proxy Class
The proxy controls access to the real object. It creates the real object only when required.
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);
}
realImage.display();
}
}
Step 4: Use the Proxy
The client uses the proxy instead of directly accessing the real object.
public class ProxyPatternDemo {
public static void main(String[] args) {
Image image = new ProxyImage("sample.jpg");
// Image will be loaded from disk
image.display();
System.out.println();
// Image will not be loaded again
image.display();
}
}
Output Explanation
When display() is called for the first time, the image is loaded from disk.
On the second call, the already loaded image is reused.
This improves performance and saves resources.
Advantages of Proxy Design Pattern
- Provides controlled access to objects
- Improves performance using lazy loading
- Adds security without modifying real object code
- Follows the Open/Closed Principle
Disadvantages of Proxy Design Pattern
- Increases complexity due to additional classes
- May slightly reduce performance due to extra indirection
When to Use Proxy Design Pattern?
- When object creation is expensive
- When access control is required
- When working with remote objects
- When you want to add logging or monitoring
Conclusion
The Proxy Design Pattern is useful when you want to control access to an object without changing its actual implementation. It is widely used in real-world applications for security, performance optimization, and resource management.