Master Java Proxy Pattern: Static vs Dynamic Implementations Explained
This article explains the Java Proxy design pattern, its three core roles, static and dynamic implementation approaches, provides a complete code example, and outlines typical scenarios where proxies improve access control, functionality, and system decoupling.
Java Proxy pattern is a structural design pattern that controls access to objects by inserting a proxy between client and real subject.
Implementation of Java Proxy Pattern
The pattern involves three roles: Subject (abstract role), RealSubject (real role), and Proxy (proxy role) as illustrated below.
Subject: defines a common interface for both proxy and real objects.
RealSubject: implements the interface and contains the actual business logic.
Proxy: holds a reference to RealSubject, controls access, and can add behavior before/after delegating to the real object.
Proxy can be implemented in two ways:
1. Static Proxy
Static proxy classes are written manually at compile time. They are simple and easy to understand but lack flexibility; a new proxy class must be created for each new subject.
2. Dynamic Proxy
Dynamic proxies are generated at runtime using Java reflection, eliminating the need to write proxy classes manually. Java provides two dynamic proxy mechanisms: interface‑based and class‑based.
Interface‑based dynamic proxy: both proxy and real class implement the same interface; the proxy holds a reference to the real object and forwards calls.
Class‑based dynamic proxy: the proxy class extends the real class (or a subclass) and overrides methods to add behavior.
Compared with static proxy, dynamic proxy reduces coupling and boilerplate code.
Java Proxy Pattern Example
The following code demonstrates a static proxy implementation.
interface Subject {
void doSomething();
}
class RealSubject implements Subject {
public void doSomething() {
System.out.println("RealSubject doSomething.");
}
}
class ProxySubject implements Subject {
private Subject realSubject;
public ProxySubject(Subject realSubject) {
this.realSubject = realSubject;
}
public void doSomething() {
System.out.println("ProxySubject doSomething.");
realSubject.doSomething();
}
}
public class Client {
public static void main(String[] args) {
Subject realSubject = new RealSubject();
Subject proxySubject = new ProxySubject(realSubject);
proxySubject.doSomething();
}
}Key points of the example:
Subject interface defines the common method.
RealSubject implements the actual logic.
ProxySubject also implements Subject and delegates to RealSubject after optional extra behavior.
Client creates both RealSubject and ProxySubject and invokes the method through the proxy.
Typical Use Cases
Proxy pattern is useful for controlling access, adding functionality such as logging, caching, validation, and reducing system coupling by separating client code from concrete implementations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
