Everything you need to know about Proxy Design Pattern

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.

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

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.
You may use a proxy while performing an expensive operation like communication with third party, perhaps over the network. The proxy would allow you to hold your data until you are ready to commit, and can limit the amount of times that the communication is called. The proxy is also useful if you want to decouple actual implementation code from the access to a particular library. Proxy is also useful for access to large files, or graphics. By using a proxy, you can delay loading the resource until you really need the data inside. Without the concept of proxies, an application could be slow, and appear non-responsive.
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.

Cons of Proxy pattern

Sometimes proxy object may contains some logic with network calls. Since proxy object has to comply with the interface of original object it may not throw checked exception. To handle such exceptions(network error, time-outs, resource not available), proxy object throws unchecked exception which the client might not be aware of. So at times, there are cases where proxy pattern increases the complexity of code or may turn out to be little inefficient.

Related

programming 1250176021073061599

Post a Comment

emo-but-icon

 

Recent Posts

comments

Join Us

 

Recommended

item