Proxy design pattern — java — get the gist in 2 min

aditya chaudhari
JavaDeveloperDiary — JDD
3 min readApr 25, 2023
understanding the proxy design pattern- img source : youtube

Imagine you are one of the gold man as shown in the image above, you are wearing very precious ornaments the whole day, you definitely would want to protect yourself and your ornaments from the thieves, and bad boys around.

What would you do?? as these men have done you can think of appointing guards around you, so anyone who wants access to you has to first go through guards to check their true identity and then they can reach you.

This example may sound funny but the whole point I want to make is, while designing the structure of your classes/methods adding an additional layer on top of important classes or behaviors (methods) can solve many problems related to security, caching, performance, etc.

The intent of the proxy design pattern is to Provide a surrogate or placeholder for another object to control access to it.
It is a structural design pattern that is also called a “surrogate”.

Think of a proxy pattern when you want to add an extra layer of indirection to access a resource or control access to it. It is useful in scenarios where you would want to improve performance by caching frequently used objects, reduce network traffic by implementing lazy loading, or provide a simplified interface to a complex system.

Simple example :
We have a requirement to cache the productPrice to save external systems calls everytime to fetch the price. When first time price is received from the external system then that price must get cached and for later calls price must be send from cached value.

public class ProxyPatternSimpleMain {
public static void main(String[] args) {

ProductProxy productProxy1 = new ProductProxy(new ConcreteProduct("iPhone", 1000));

// First call gets price from the Product object and caches it
System.out.println(productProxy1.getPrice());

// Second call returns the cached price
System.out.println(productProxy1.getPrice());
}
}

**
public class ProductProxy implements Product {
****** some code here ****

public int getPrice() {
if (this.priceCache == null) {
System.out.println("calling different system to get price first time");
this.priceCache = this.product.getPrice();
}

return this.priceCache;
}
}

complete code github link

Structure

structure of proxy pattern — sources : wiki

The Proxy design pattern consists of four main components:

  1. Subject: This is the interface that defines the object that we want to access or control access to.
  2. RealSubject: This is the object that we want to control access to. It implements the Subject interface and provides the actual implementation of the operations defined in the interface.
  3. Proxy: This is the object that stands between the client and the real subject. It implements the same interface as the real subject and delegates the operations to the real subject. The proxy can also add additional functionality, such as caching or security checks, before and after delegating to the real subject.
  4. Client: This is the object that uses the proxy to access the real subject.

In the Spring Framework, AOP and transaction modules use the the proxy pattern
In AOP(Aspect Oriented Programming), proxies are used to intercept method calls and provide cross-cutting concerns, such as logging and security checks.
In transaction management, proxies are used to control access to database transactions and provide rollback and commit functionality.

The real fun is in the details, if you wish to know and understand the pros-cons, java-code example for the proxy pattern then please consider checking the detailed article at [proxy-pattern-explained-9-min-read]

On the journey to understand the core essence of design patterns.
Every Tuesday I publish a small 2 min post and a long 5–10 min post along with a code example, please consider following if you wish to get notified!

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT