Thursday, March 3, 2011

Proxy Pattern

Proxy Pattern provides a surrogate or placeholder for another object to control the access. There are number of ways it can manage the access.
It means we are representing a complex object with a simpler one.

The Need:
  • Control access to an object
  • Examples:
    • The object may be expensive to use (e.g., the manipulation of an image) and it may not be necessary to do so (e.g., because the image is not visible or because you are using a thumbnail)
    • The object being accessed may reside on a "remote"  address space/machine  and special "communications" may be required
    • The object being accessed may be "priveleged" (e.g., for security reasons) in some applications and not in others
Proxy Pattern

Proxy pattern category
-> A RemoteProxy manages interaction between a client and a remote object.
-> A Virtual Proxy controls access to an object that is expensive to instantiate.
-> A Protection Proxy controls access to the methods of an object based on the caller.
-> A smart proxy interposes additional actions when an object is accessed. Typical uses include:
1. Counting the number of references to the real object so that it can be freed automatically when there are no more references
2. Loading a persistent object into memory when it's first referenced
3. Checking that the real object is locked before it is accessed to ensure that no other object can change it.
Even many other variants of Proxy Pattern exists.
When to use:
1. Use the proxy Pattern in situations where client does not or can not reference an
object directly, but wants to still interact with the object
2. Use the proxy Pattern to create a representative object that controls access to another object, which may be remote, expensive to create or in need of securing.
-> The proxy object has the same interface as the target object
-> The proxy holds a reference to the target object and can forward requests to
the target as required (delegation!)
-> In effect, the proxy object has the authority the act on behalf of the client to
interact with the target object
To make clients use Proxy rather than the Real One, provide a factory that instantiates and returns the subject. Because this happens in a factory method we can then wrap the real one with a proxy before returning it. The client never knows or cares that it's using a proxy instead of the real thing.
Let us take a real time use of Proxy Pattern as an example.
By the way this code bit is from my Personal Project...
This is an example of static Proxy.

Java's built-in support for Proxy can build a dynamic proxy class on demand and dispatch all calls on it to a handler of your choosing.
Like any wrapper, proxy will increase the number of classes and objects in your designs.
Rules of thumb
1. Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface. [GoF. p216]
2. 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. [GoF, p220]
3. Proxy controls access to objects . But Decorator decorates the objects by adding behaviour to them.

No comments:

Post a Comment