> For the complete documentation index, see [llms.txt](https://jrp.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jrp.gitbook.io/notes/design-pattern/structural-proxy-pattern.md).

# Structural - Proxy Pattern

<div data-with-frame="true"><figure><img src="/files/kBvga9J0BZYy2X1wb6TD" alt=""><figcaption></figcaption></figure></div>

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

<div data-with-frame="true"><figure><img src="/files/qhIEvMTHEU7TZossDYog" alt=""><figcaption><p>Proxy Pattern</p></figcaption></figure></div>

### Implementation

#### Interface

```java
public interface EmployeeDao {
	void create();
}
```

#### Implementation Class

```java
public class EmployeeDaoImpl implements EmployeeDao {
	@Override
	public void create() {
		System.out.println("Connecting Employee Dao via EmployeeDaoImpl");
	}
}
```

#### Proxy Class

```java
public class EmployeeDaoProxy {
	EmployeeDao employeeDao;

	EmployeeDaoProxy() {
		employeeDao = new EmployeeDaoImpl();
	}

	void createEmployee() {
		employeeDao.create();
	}
}
```

#### Client Class

```java
public class ProxyClient {

	public static void main(String[] args) {
		EmployeeDaoProxy proxy = new EmployeeDaoProxy();
		proxy.createEmployee();
	}
}
```

#### Output

```
Connecting Employee Dao via EmployeeDaoImpl
```

Pretty Simple, isn't it?

<mark style="color:purple;background-color:$info;">**Example in JDK :**</mark>  <mark style="color:purple;background-color:$info;">**java.lang.reflect.Proxy**</mark><mark style="color:purple;background-color:$info;">.</mark>

* <mark style="color:purple;background-color:$info;">This class lets you create dynamic proxy objects that implement interfaces and can intercept, manage, or delegate method calls to a real object.</mark>
* <mark style="color:purple;background-color:$info;">You can add access control, logging, remote invocation, or security checks without changing the real object.</mark>

**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.
