DEV Community

Priyank Bhardwaj
Priyank Bhardwaj

Posted on

Proxy Design Pattern in Java: Your Middleman for Performance & Security!

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

  1. Virtual Proxy – Lazy load heavy objects.
  2. Protection Proxy – Control access with authentication/authorization.
  3. Remote Proxy – Act as local representative for remote objects (like Remote Method Invocation).
  4. Caching Proxy – Cache results for efficiency.
  5. Smart Proxy – Add extra actions (logging, monitoring, reference counting).

Structure (UML Breakdown)

The Proxy pattern has three main roles:

  1. Subject (Interface/Abstract class): Common interface for both RealSubject and Proxy.

  2. RealSubject: The actual object that does the real work.

  3. Proxy: Acts as a placeholder, controls access to RealSubject.

Client → Subject → Proxy → RealSubject
Enter fullscreen mode Exit fullscreen mode

Example implementation

Step 1: Create Subject Interface

public interface Image {
    void display();
}
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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();
    }
}

Enter fullscreen mode Exit fullscreen mode

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Loading photo.jpg
Displaying photo.jpg

Displaying photo.jpg

Enter fullscreen mode Exit fullscreen mode

Real-World Examples of Proxy in Java

  1. java.lang.reflect.Proxy → Dynamic proxies for implementing interfaces at runtime.
  2. Spring AOP Proxies → Used for method interception (logging, transactions, security).
  3. 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)