Everything you need to know about Proxy Design Pattern

https://www.coolcoder.in/2015/07/everything-you-need-to-know-about-proxy.html
In computer networks, a proxy server is a server (a computer system or an application) that acts as an intermediary for requests
from clients seeking resources from other servers. A client connects to the proxy server, requesting some service, such as a file,
connection, web page, or other resource available from a different server and the proxy server evaluates the request as a way to
simplify and control its complexity. So the proxy server is an interface to the end-user or application which internally connects to other server to serve the user/application request.
In this case, Subject is an interface which the client uses for interaction with Real Subject. Both the Proxy and RealSubject objects implement the Subject interface, but the client uses the instance of Proxy object to perform the action.
Let's take a look at this in action with a sequence diagram
Proxy Design Pattern
In computer programming, the same concept evolved as a software design pattern. The proxy pattern is a structural design pattern. It provides a surrogate or placeholder for another object to control access to it. It is as an object(proxy object) having original object(Real Subject) to interface its functionality to outer world. Outer world uses the proxy object to fulfil all of its needs. Proxy object is a wrapper object which has the real subject as a member variable or a method variable. So the proxy object has full control over methods that are to be invoked on the real subject. It's very clear that the Proxy consists of a RealSubject object and it has full control over the life-cycle of that RealSubject object. The Proxy object may internally use the instance of RealSubject to perform the action.
Let's take a look at this in action with a sequence diagram
Need of Proxy Design Pattern
The proxy design pattern allows you to provide an interface to other objects by creating a wrapper class as the proxy. The wrapper class, which is the proxy, can add additional functionality to the object of interest without changing the object's code. This pattern is recommended when either of the following scenarios occur in your application.- Access control for the original object is required. Adding authorization to an existing object. The proxy will determine if the client can access the object of interest.
- Lazy-initialization of objects. Objects need to be created on demand.
- Simplifying the API of complex objects. The proxy can provide a simple API so that the client code does not have to deal with the complexity of the object of interest.
- Adding a thread-safe feature to an existing class without making chnages to the existing class's code.
- Providing interface for remote resources, such as web service or REST resources.
- The object being represented is external to the system.
- Additional functionality is required when an object is accessed.
public interface Subject { public void doAction(); }Below is its implementation as RealSubject
public class RealSubject implements Subject { public RealSubject() { //Default Constructor } public void doAction() { //Perform Action } }Now the ProxySubject too implements the Subject interface. But while performing action, it delegates to the RealSubject by invoking RealSubject's doAction method. The RealSubject object may be member or a method variable as per its usage.
public class ProxySubject implements Subject { public ProxySubject() { //Default Constructor } //this method delegates to the real subject public void doAction() { RealSubject realSubject = new RealSubject(); realSubject.doAction(); } }Now the client uses the reference of interface to invoke action. Since the reference will contain an instance of ProxySubject, it will have full control over the RealSubject.