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)