🛡️ Proxy Design Pattern in Java – Simplifying Access & Control

🛡️ Proxy Design Pattern in Java – Simplifying Access & Control

Imagine you live in a gated community. 🏡

  • You don’t directly deal with every visitor.
  • Instead, a security guard (proxy) checks the visitor first and only allows them in if they are authorized.
  • The house (real subject) is protected behind the proxy (guard).

This is exactly what the Proxy Design Pattern does — it controls access to an object, adding a layer of security, caching, logging, or remote communication.


🔹 Professional Definition

The Proxy Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it.

It is widely used to:

  • Add lazy initialization (create expensive objects only when needed).
  • Introduce access control or authentication.
  • Manage remote calls (Remote Proxy).
  • Provide logging, caching, and monitoring transparently.


🔹 Real-World Example (Layman Friendly)

Think of ATM machines 🏧:

  • When you insert your card, you’re not directly talking to the bank’s core server.
  • Instead, the ATM acts as a proxy.
  • It validates your card, checks your PIN, and forwards only valid requests to the bank’s server.

This ensures: ✅ Security ✅ Controlled access ✅ Simplified interaction for the user


🔹 Java Example (Simple)

// Subject
interface Internet {
    void connectTo(String serverHost) throws Exception;
}

// Real Subject
class RealInternet implements Internet {
    public void connectTo(String serverHost) {
        System.out.println("Connecting to " + serverHost);
    }
}

// Proxy
class ProxyInternet implements Internet {
    private Internet internet = new RealInternet();
    private static List<String> bannedSites;

    static {
        bannedSites = new ArrayList<>();
        bannedSites.add("abc.com");
        bannedSites.add("xyz.com");
    }

    public void connectTo(String serverHost) throws Exception {
        if(bannedSites.contains(serverHost.toLowerCase())) {
            throw new Exception("Access Denied to " + serverHost);
        }
        internet.connectTo(serverHost);
    }
}

// Client
public class ProxyPatternDemo {
    public static void main(String[] args) {
        Internet internet = new ProxyInternet();
        try {
            internet.connectTo("google.com");  // Allowed
            internet.connectTo("abc.com");     // Blocked
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
        

🔎 Output:

Connecting to google.com
Access Denied to abc.com
        

🔹 Production-Level Use Case (Java & Enterprise)

In production, Proxy Pattern is heavily used in Spring AOP (Aspect-Oriented Programming), Hibernate Lazy Loading, and Microservices Security Gateways.

👉 Example: Database Lazy Loading with Hibernate

  • When you fetch an entity from the database, not all related objects are loaded immediately.
  • Instead, Hibernate uses a proxy object for related entities.
  • When you actually access the related object, Hibernate loads it from the database on-demand.

This improves performance and avoids unnecessary DB queries.


🔹 Key Benefits

✅ Controls access to sensitive objects

✅ Improves performance (lazy loading & caching)

✅ Adds logging, security, or monitoring without modifying core logic

✅ Essential for distributed systems (remote proxies, API gateways)


📌 Conclusion

The Proxy Pattern is like a smart gatekeeper — it gives you controlled, secure, and efficient access to resources. From banning sites in a browser to on-demand loading in Hibernate, it plays a crucial role in real-world Java applications.

Suraj Singh Nice one! What’s often missed is how Spring actually intercepts method calls and manages transactions via proxies. Seeing that flow changes everything 👇 https://www.garudax.id/posts/shivani-m-6bbb5621b_11howtransactionalworksinternally-activity-7439570000593702912-kiOB

Like
Reply

To view or add a comment, sign in

More articles by Suraj Singh

Others also viewed

Explore content categories