Proxy pattern
Definition
Provide a surrogate or placeholder for another object to control access to it.
It enables us to create a stand-in or replacement for another object. Using a proxy, you may conduct an action either before or after the request reaches the original object, controlling access to it.
The main intent of a proxy is to control access to the target object, rather than to enhance the functionality of the target object. The access control includes
- synchronization,
- authentication,
- remote access (RPC),
- lazy instantiation (Hibernate, Mybatis),
- AOP (transaction).
In contrast with static proxy, the dynamic proxy generates bytecode which requires Java reflection at runtime. With the dynamic approach you don’t need to create the proxy class, which can lead to more convenience.
Problem
Why would you want to restrict who has access to something? Imagine that you have a large item that uses a lot of system resources. You don’t always need it, but sometimes you do. One way to solve it by using lazy initialization, which would allow you to build an object only when one is required. Some postponed initialization code would have to be executed by each client of the object. Unfortunately, there would likely be a great deal of code duplication.
So if you are working on a system and feel the need to introduce a proxy either to instantiate a real object on demand, or hide the fact that the object is running on a remote location, or control access to the object, you can easily do so. You don’t need to worry about the client being aware about any changes from the introduction of the proxy. The client will keep using the same interface thinking it is interacting with the real object, while the proxy will be mediating in between.
Solution
Using the proxy design pattern, develop a new proxy class that has the same interface as a primary service object. You then update your app to pass the proxy object to all the clients of the original object. The proxy builds an actual service object and transfers all responsibility to it after receiving a request from a client.
Participants of the Proxy Pattern
With the basic class structure of the application set up, we can relate them with the participants of the Proxy pattern as:
- Subject (ReportGenerator): Is an interface for both the real object and its proxy. The Subject enables the proxy to be used anywhere the real object is expected.
- RealSubject (ReportGeneratorImpl): The real object that is expensive to create, requires protection, or running on a remote JVM. RealSubject implements Subject and we create proxy of it.
- Proxy (ReportGeneratorImplProxy): Implements Subject and maintains a reference to RealSubject.
Class diagram

Real world examples
- In hibernate, we create the code to retrieve entities from the database. Hibernate returns an object that serves as a proxy for the underlying entity class. It does this by dynamically extending the domain class. The client software can use the proxy to read any data it requires. With the use of these proxy entity classes, we can implement lazy loading scenarios in which it only fetches the required entities when they are specifically required. It aids in enhancing the effectiveness of DAO activities.
- It protects internet access via a network proxy on business networks. All network queries go through a proxy, which checks them for requests from domains that are approved before posting the data to the network. If a request appears suspicious, the proxy whould stop it; otherwise, the request whould go through.
- An object produced by the AOP framework in aspect-oriented programming (AOP) to carry out the aspect contracts (advise method executions and so on). For instance, a JDK dynamic proxy or a CGLIB proxy would be an AOP proxy in the Spring AOP.
Different Proxies
- Remote proxy -> depicts a distantly lactated object. The client must put more effort into network connection if they want to communicate with distant objects. Clients concentrate on having genuine conversations while a proxy object handles this communication on the original object’s behalf.
- Virtual proxy -> delays the on-demand generation and initialization of expensive objects before they are needed. Virtual proxies include entities produced by Hibernate.
- Protection proxy -> helps put in place security over the original object. Before calling a method, they might check the access privileges and then grant or refuse access according to the results.
- Smart Proxy -> conducts additional clerical work whenever a client accesses an object. An illustration would be to confirm that it locked the actual object before accessing it to make sure that no other object may alter it.
Advantages
- Security is one benefit of the proxy pattern.
- This design prevents the duplication of potentially enormously large and memory-intensive items. This improves the application’s performance.
- By installing a local code proxy (stub) on the client computer and then connecting to the server using remote code, the remote proxy also assures security.
Disadvantages
- This pattern adds another layer of abstraction, which can occasionally cause problems if some clients access the Real subject code directly while others might access the Proxy classes. This could cause inconsistent behavior.
When to Use Proxy Design Pattern?
- To provide a surrogate or placeholder for another object to control access to it.
- To support distributed, regulated, or intelligent access, add another indirection.
- To prevent the real component from becoming overly complex, add a wrapper and delegation.
Proxy vs Decorator Pattern
The major distinction between the two patterns is the burdens each pattern carries. Decorators concentrate on adding duties, whereas proxies concentrate on restricting access to an object.
Reading material
- https://springframework.guru/gang-of-four-design-patterns/proxy-pattern/
- https://www.javadevjournal.com/java-design-patterns/proxy-design-pattern/