Structural - Proxy Pattern
The proxy acts as an intermediary, controlling access to the real object

Imagine you have a toy box in your room, but your mom doesn’t want you to open it by yourself. So, she tells you, “If you want a toy, ask me and I will open the box for you.” Here, your mom is like a proxy—she stands between you and the toy box. You can’t directly open it, but she makes sure you get what you want (and keeps things safe).
In computer code, the proxy pattern works just like your mom helping you get the toys. There is a helper (proxy) who controls and manages access to the real thing (toy box).
Proxy helps you talk to the real thing.
The real object still does the main work.
The helper (proxy) can also check or control what you get.
Low Level Design

Implementation
Interface
public interface EmployeeDao {
void create();
}Implementation Class
public class EmployeeDaoImpl implements EmployeeDao {
@Override
public void create() {
System.out.println("Connecting Employee Dao via EmployeeDaoImpl");
}
}Proxy Class
public class EmployeeDaoProxy {
EmployeeDao employeeDao;
EmployeeDaoProxy() {
employeeDao = new EmployeeDaoImpl();
}
void createEmployee() {
employeeDao.create();
}
}Client Class
public class ProxyClient {
public static void main(String[] args) {
EmployeeDaoProxy proxy = new EmployeeDaoProxy();
proxy.createEmployee();
}
}Output
Connecting Employee Dao via EmployeeDaoImplPretty Simple, isn't it?
Example in JDK : java.lang.reflect.Proxy.
This class lets you create dynamic proxy objects that implement interfaces and can intercept, manage, or delegate method calls to a real object.
You can add access control, logging, remote invocation, or security checks without changing the real object.
Advantages of the Proxy Pattern:
Controlled Access: The proxy controls access to the real object, adding a layer of security or checks before allowing use.
Lazy Initialization: The proxy can create and initialize the real object only when needed, which saves resources.
Logging and Monitoring: You can add logging, monitoring, or caching in the proxy without modifying the real object’s code.
Remote Access: The proxy lets you access objects in a different location (like on another computer), pretending they are local.
Disadvantages of the Proxy Pattern:
Added Complexity: Introducing a proxy adds extra code and complexity to your system. You have more classes to manage.
Slower Performance: Sometimes, the proxy can make things a bit slower due to the extra step in communication.
Tight Coupling: If the proxy is not designed carefully, it can become tightly coupled to the real object, reducing flexibility.
Last updated
Was this helpful?