Friday 25 October 2013

Proxy Design Pattern Notes

you may see similarities between the Decorator and Proxy design patterns. Both patterns use a proxy that forwards method calls to another object, known as the real subject. The difference is that, with the Proxy pattern, the relationship between a proxy and the real subject is typically set at compile time, whereas decorators can be recursively constructed at runtime. 

Proxy: Control access to an object with a proxy (also known as a surrogate or placeholder).
one use for the Proxy design pattern: accessing remote objects. Proxies also prove useful for creating expensive resources on demand, a virtual proxy, and for controlling access to objects, a protection proxy.

The proxy forwards that method call to the Web service, which often resides on a different machine than that of the client.

n situations where multiple copies of a complex object must exist, the proxy pattern can be adapted to incorporate the flyweight pattern in order to reduce the application's memory footprint. Typically, one instance of the complex object and multiple proxy objects are created, all of which contain a reference to the single original complex object. Any operations performed on the proxies are forwarded to the original object. Once all instances of the proxy are out of scope, the complex object's memory may be deallocated.
So it's quite a simple concept - to save on the amount of memory used, you might use a Proxy. Similarly, if you want to control access to an object, the pattern becomes useful.
Proxy is required whenever there is need for more sophisticated or versatile reference to an object than a simple pointer.Here are some situations when proxy pattern is applicable.
  1. remote proxy provides a local representative for an object in a different address space.Providing interface for remote resources such as web service or REST resources.
  1. virtual proxy creates expensive object on demand.
  1. protection proxy controls access to the original object.Protection proxies are useful when objects should have different access rights.
  1. smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
  1. Adding a thread-safe feature to an existing class without changing the existing class's code. 
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#mDPSt6h12coDuSLm.99

You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.


Rules of thumb


  • Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
  • Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.
Provide a surrogate or placeholder for another object to control access to it. 


Related Patterns

  • Adapter Design Pattern - The adapter implements a different interface to the object it adapts where a proxy implements the same interface as its subject.
  • Decorator Design Pattern - A decorator implementation can be the same as the proxy however a decorator adds responsibilities to an object dynamically while a proxy controls access to it.


  • Java RMI as has been explained implements a remote proxy
  • Security Proxies that controls access to objects can be found in many object oriented languages including java, C#, C++.

  • The Proxy changes the behavior of the Service, but preserves its interface.
  • The Adapter changes the interface of the Service, but preserves it behavior.

  • The purpose of the Proxy is to add behavior. The purpose of the Facade is to simplify, which may actually involve removing behavior


Proxy vs. Adapter

  • The Proxy changes the behavior of the Service, but preserves its interface.
  • The Adapter changes the interface of the Service, but preserves it behavior.
  • A Client can use the Proxy or the Service Entity in the same way.
  • A Client designed to use the Adapter would not be able to use the Service Entity without it.
  • The Proxy can be cast to the interface of the Service. The Adapter can be cast to the interface the Client expects.

Adapter vs. Facade

  • The Adapter is used to preserve existing polymorphism. In other words, the interface you are adapting to probably already exists, and is probably already committed to.
  • The Facade provides a more idealized interface, but it can be developed incrementally as new needs of the subsystem are discovered.
  • Adapters are usually small, and therefore do not raise performance concerns, typically.
  • Facades tend to be large, and therefore it may be useful to make sure they are re-entrant (so as to avoid the need for multiple instances).
  • The "Adaptee" is probably fine as it is, you're using the Adapter because you've committed to a different interface.
  • The "Facadee" is probably not fine as it is, and you are communicating this to other developers by using the term "Facade".

Proxy vs. Facade

  • Proxies are optional, Facades typically are not.
  • The purpose of the Proxy is to add behavior. The purpose of the Facade is to simplify, which may actually involve removing behavior

No comments:

Post a Comment