Control access to an object by wrapping it with a proxy โ add logging, caching, rate limiting, or access control transparently.
๐ง What is the Proxy Pattern?
The Proxy Design Pattern is a structural pattern that provides a placeholder or surrogate object to control access to another object.
โ
Purpose:
Add a layer of control without modifying the actual object.
๐ฏ When to Use It?
Lazy initialization (expensive object)
Access control (e.g. admin-only)
Caching / throttling
Logging or audit trail
Remote object communication (e.g. RPC, RMI)
๐งฑ Participants
Component
Role
Subject (Interface)
Declares the common operations
RealSubject
The actual implementation
Proxy
Controls access to RealSubject, implements same interface
๐ก Real-World Examples
Use Case
Description
๐ชช Authorization Proxy
Check if user is allowed to invoke action
๐ Remote Proxy
Represent a remote object locally (e.g., RMI, gRPC)
๐ฆ Virtual Proxy
Delay object creation until actually needed
๐ง Smart Proxy
Add logging, caching, metrics, or reference counting
๐ป Java Code Example โ Access Control Proxy
Step 1: Common Interface
public interface ReportService {
void generateReport ();
}
Enter fullscreen mode
Exit fullscreen mode
Step 2: Real Implementation
public class ReportServiceImpl implements ReportService {
@Override
public void generateReport () {
System . out . println ( "โ
Generating sensitive financial report..." );
}
}
Enter fullscreen mode
Exit fullscreen mode
Step 3: Proxy Implementation
public class ReportServiceProxy implements ReportService {
private final ReportService realService ;
private final String currentUser ;
public ReportServiceProxy ( String currentUser ) {
this . realService = new ReportServiceImpl ();
this . currentUser = currentUser ;
}
@Override
public void generateReport () {
if ( "admin" . equalsIgnoreCase ( currentUser )) {
realService . generateReport ();
} else {
System . out . println ( "โ Access Denied: Only admin can generate reports." );
}
}
}
Enter fullscreen mode
Exit fullscreen mode
Step 4: Client Code
public class ProxyPatternDemo {
public static void main ( String [] args ) {
ReportService adminProxy = new ReportServiceProxy ( "admin" );
adminProxy . generateReport (); // โ
Allowed
ReportService userProxy = new ReportServiceProxy ( "john" );
userProxy . generateReport (); // โ Denied
}
}
Enter fullscreen mode
Exit fullscreen mode
๐งช Output
โ
Generating sensitive financial report...
โ Access Denied: Only admin can generate reports.
Enter fullscreen mode
Exit fullscreen mode
๐ Advanced Proxy Use Cases
Feature
Implementation
๐ข Lazy-loading
Load object only when needed (e.g., HibernateProxy)
โก Caching
Proxy stores previous results and avoids re-computation
๐ Security
Restrict certain API calls to specific roles
๐ RPC
Remote object is proxied locally (e.g., Stub)
๐งพ Logging
Automatically log every method call and argument
๐งฐ In the Real World
Framework/Library
Proxy Use
Spring AOP
Logging, transactions, security via proxies
Hibernate
Lazy-loading entity proxies
Retrofit / Feign
HTTP service client proxy
gRPC / RMI
Remote proxies
Java Dynamic Proxy API
java.lang.reflect.Proxy
๐ Static vs Dynamic Proxy
Type
Description
Example
Static Proxy
You write the proxy manually
ReportServiceProxy
Dynamic Proxy
Created at runtime using reflection
Proxy.newProxyInstance(...) in Java
๐งพ Summary
Feature
Proxy Pattern
โ
Structure Type
Structural
๐ฏ Used For
Access control, lazy loading, logging
๐ง Interface-Based
Yes
๐ Transparent to Client
Yes
Top comments (0)