100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the Proxy Pattern?

Learn the Proxy pattern — virtual, protection, remote and caching proxies — with a Java lazy-loading example and interview Q&A.

mediumQ35 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

The Proxy pattern provides a surrogate object that implements the same interface as a real subject and controls access to it, allowing extra logic such as lazy loading, access control, or logging to run before or after delegating to the real object.

Because the proxy and the real subject share a common interface, clients interact with either one interchangeably without knowing which they hold. Common proxy variants include virtual proxies that defer expensive object creation until it is actually needed, protection proxies that check permissions before forwarding a call, remote proxies that represent an object living in another process or machine, and caching proxies that store results of expensive operations. Unlike Decorator, whose intent is to add behavior, Proxy’s intent is to control access to the underlying object, though the structural implementation (wrap-and-delegate) looks similar.

  • Defers expensive object creation until actually needed
  • Enforces access control transparently to the client
  • Enables remote or cross-process objects to look local
  • Adds caching or logging without changing the real subject

AI Mentor Explanation

A team’s press officer speaks to journalists on a star player’s behalf, deciding which questions get forwarded to the player directly and which get answered or deflected without bothering them. Journalists interact with the press officer as if speaking to the player, but the officer controls exactly what reaches the real subject. That is the Proxy pattern: a stand-in object implementing the same “point of contact” role controls access to the real object behind it.

Step-by-Step Explanation

  1. Step 1

    Define the shared subject interface

    Both the real object and the proxy implement the same interface clients depend on.

  2. Step 2

    Implement the real subject

    Write the class containing the actual, potentially expensive or restricted logic.

  3. Step 3

    Implement the proxy

    It holds a reference (possibly lazily created) to the real subject and implements the same interface.

  4. Step 4

    Add the proxy's extra behavior

    Insert lazy instantiation, permission checks, caching, or remote marshaling around the delegated call.

What Interviewer Expects

  • Correct distinction between virtual, protection, remote, and caching proxies
  • Understanding that the proxy shares the real subject's interface
  • Clear differentiation from Decorator (access control vs behavior addition)
  • A concrete example, such as lazy image loading or a Java RMI stub

Common Mistakes

  • Treating Proxy and Decorator as the same pattern because both wrap an object
  • Forgetting the proxy must implement the exact interface clients expect
  • Assuming Proxy always adds new business behavior rather than controlling access
  • Not mentioning any concrete proxy variant (virtual, protection, remote, caching)

Best Answer (HR Friendly)

The Proxy pattern puts a stand-in object in front of the real object, and both implement the same interface, so the client can’t tell the difference. The proxy can delay creating the expensive real object until it’s needed, check permissions before letting a call through, or cache results — all while looking exactly like the real thing to whoever is calling it.

Code Example

Virtual proxy for lazy image loading
interface Image {
    void display();
}

class RealImage implements Image {
    private String filename;
    RealImage(String filename) {
        this.filename = filename;
        loadFromDisk(); // expensive
    }
    private void loadFromDisk() { System.out.println("Loading " + filename); }
    public void display() { System.out.println("Displaying " + filename); }
}

class ImageProxy implements Image {
    private String filename;
    private RealImage real;
    ImageProxy(String filename) { this.filename = filename; }
    public void display() {
        if (real == null) real = new RealImage(filename); // created only when needed
        real.display();
    }
}

Image image = new ImageProxy("photo.png"); // no disk load yet
image.display(); // real image created and shown now

Follow-up Questions

  • What is the difference between a virtual proxy and a protection proxy?
  • How does Proxy differ from Decorator structurally versus by intent?
  • How does Java's dynamic proxy (java.lang.reflect.Proxy) relate to this pattern?
  • What is a remote proxy and where have you seen one used?

MCQ Practice

1. The Proxy pattern's primary intent is to?

Proxy's core intent is controlling access to a real subject, unlike Decorator, whose intent is adding behavior.

2. A proxy that delays creating an expensive object until first use is called a?

A virtual proxy defers instantiation of the real, costly subject until it is actually needed.

3. A proxy that checks permissions before forwarding a call is called a?

A protection proxy validates access rights before delegating the call to the real subject.

Flash Cards

Proxy pattern in one line?A surrogate sharing the real subject's interface that controls access to it.

Proxy vs Decorator intent?Proxy controls access; Decorator adds behavior. Structurally they look similar.

Virtual proxy?Defers expensive object creation until it's actually needed.

Protection proxy?Checks permissions before forwarding a call to the real object.

1 / 4

Continue Learning