Sitemap
Bappaditya Roy

Bappaditya Roy

Proxy Design Pattern In Java

5 min readJul 28, 2026

--

What is the Proxy Design Pattern?

Proxy Pattern is a structural design pattern that provides a placeholder (proxy) for another object to control access to it.

In simple words:

A Proxy sits between the client and the real object and decides whether, when, or how to forward the request.

Unlike the Decorator Pattern, a Proxy does not add new business functionality. Instead, it controls access to the real object.

Press enter or click to view image in full size

Why Did Proxy Pattern Come Into Picture?

Suppose your application downloads files from cloud storage.

CloudFile file = new CloudFile();
file.download();

The problem?

Creating the CloudFile object is expensive.

  • It takes 10 seconds.
  • It establishes a network connection.
  • It authenticates with the server.
  • It consumes memory.

However, many users never actually download the file.

So why create such an expensive object immediately?

Another Problem

Suppose you’re building an Admin Portal.

admin.deleteUser();

Should every user be allowed to execute this?

No.

Only administrators should.

Someone must verify permissions before forwarding the request.

Another Problem

Suppose your application calls an external API.

Each API request costs money.

If the same request is made repeatedly, you don’t want to call the API every time.

Instead, you want to return cached data whenever possible.

Again, you need an object that decides whether to call the real service.

Problems Solved by Proxy

The Proxy Pattern controls access to the real object.

Common use cases include:

  • Lazy Loading
  • Security
  • Caching
  • Logging
  • Remote Objects
  • Rate Limiting

Real-Life Example

Imagine you want to meet a company’s CEO.

Can you directly walk into the CEO’s office?

No.

You first meet the secretary.

You
↓
Secretary (Proxy)
↓
CEO

The secretary checks

  • Do you have an appointment?
  • Is the CEO available?
  • Are you authorised?

Only then are you allowed to meet the CEO.

The secretary does not become the CEO.

She simply controls access.

That is exactly what a Proxy does.

Structure

Internet (Interface)
▲
│
--------------------------
│ │
RealInternet ProxyInternet

Notice something important.

Both classes implement the same interface.

The Proxy contains a reference to the Real Object.

Step 1 — Create the Interface

public interface Internet {
void connect(String website);
}

Both the Proxy and the Real Object implement this interface.

Step 2 — Create the Real Object

public class RealInternet implements Internet {
void connect(String website);
}

This class performs the actual work.

Step 3 — Create the Proxy

public class ProxyInternet implements Internet {
private RealInternet internet = new RealInternet();
@Override
public void connect(String website) {
if (website.equals("facebook.com")) {
System.out.println("Access Denied");
return;
}
internet.connect(website);
}
}

The Proxy first decides whether access should be granted.

If allowed,

it forwards the request to the real object.

Otherwise,

it blocks the request.

Step 4 — Main Class

public class Main {
public static void main(String[] args) {
Internet internet = new ProxyInternet();
internet.connect("google.com");
internet.connect("facebook.com");
}
}

Output

Connecting to google.com
Access Denied

The client never communicates directly with RealInternet.

Every request goes through the Proxy.

Step-by-Step Execution

Suppose Java executes

Internet internet = new ProxyInternet();
internet.connect("google.com");

Step 1

new ProxyInternet();

creates

ProxyInternet

Inside it,

private RealInternet internet = new RealInternet();

creates

RealInternet

Memory

internet
↓
ProxyInternet
↓
RealInternet

Step 2

The client calls

internet.connect("google.com");

Since the variable points to ProxyInternet,

Java executes

ProxyInternet.connect()

Step 3

The Proxy checks

if (website.equals("facebook.com"))

The website is

google.com

Access is allowed.

Step 4

The Proxy forwards the request

internet.connect(website);

which calls

RealInternet.connect()

Output

Connecting to google.com

Execution Flow

Main
↓
ProxyInternet.connect()
↓
Permission Check
↓
Allowed?
↓
Yes
↓
RealInternet.connect()
↓
Connection Established

The Proxy always receives the request first.

Get Bappaditya Roy’s stories in your inbox

Join Medium for free to get updates from this writer.

It decides whether to execute the real object.

Memory Diagram

internet
│
▼
+----------------------+
| ProxyInternet |
|----------------------|
| internet ----------┐ |
+--------------------│-+
│
▼
+----------------------+
| RealInternet |
+----------------------+

The client communicates only with the Proxy.

The Proxy communicates with the Real Object.

Lazy Loading Example

Suppose loading an image takes 5 seconds.

Without Proxy

Image image = new RealImage("photo.jpg");

The image loads immediately.

Even if the user never opens it.

Using a Proxy

Image image = new ProxyImage("photo.jpg");

No image is loaded yet.

Only when

image.display();

is called does the Proxy create

new RealImage("photo.jpg");

This is known as Virtual Proxy.

Types of Proxy

1. Virtual Proxy

Creates expensive objects only when they are actually needed.

Examples

  • Image loading
  • Video loading
  • PDF loading

2. Protection Proxy

Checks whether the caller has permission.

Examples

  • Admin pages
  • Banking systems
  • Hospital management systems

3. Remote Proxy

Represents an object that exists on another machine.

Examples

  • Java RMI
  • gRPC clients
  • REST API client wrappers

4. Cache Proxy

Checks cache before calling the real service.

If data exists,

return cached data.

Otherwise,

call the real service.

5. Logging Proxy

Logs every request before forwarding it.

Useful for auditing and monitoring.

Where Is Proxy Used?

1. Spring AOP

Spring creates Proxy objects for

  • @Transactional
  • @Cacheable
  • @Async
  • @Retryable

When you call

service.save();

you are often calling a Proxy object.

The Proxy performs additional work before and after invoking the real service.

2. Hibernate

Lazy loading.

User user = repository.findById(1);

Hibernate may return a Proxy.

The actual database query is executed only when related data is accessed.

3. Security

Before executing

deleteUser();

the Proxy checks

Is user an ADMIN?

If yes,

forward the request.

Otherwise,

reject it.

4. API Clients

A Proxy can

  • Add authentication headers
  • Retry failed requests
  • Log API calls
  • Cache responses

before invoking the external API.

5. Reverse Proxy Servers

Examples

  • Nginx
  • HAProxy
  • Cloudflare

These sit between clients and your application.

They perform

  • SSL termination
  • Load balancing
  • Caching
  • Security filtering

before forwarding requests.

Advantages

Controls access to objects.

Supports lazy loading.

Improves security.

Enables caching.

Hides remote communication.

Follows the Open-Closed Principle.

Disadvantages

Adds an extra layer of abstraction.

Slight performance overhead.

More classes to maintain.

Interview Answer

Proxy Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. The Proxy implements the same interface as the real object and decides whether, when, or how requests are forwarded.

Key Takeaways

  • Proxy and Real Object implement the same interface.
  • The client talks only to the Proxy.
  • The Proxy decides whether to call the Real Object.
  • The Proxy is mainly used for access control, lazy loading, caching, security, logging, and remote communication.
  • Unlike the Decorator Pattern, the Proxy does not change the business behaviour of the object.

--

--