Thursday, April 7, 2011

Design Pattern: Proxy

The Proxy provides a surrogate or place holder to provide access to an object. 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.

Say that you’ve got some local code that’s used to dealing with a local object as shown in the figure below

image

 

 

 

 

 

But now say that you want to deal with some remote object, somewhere else in the world. How can you make the local code think it’s dealing with a local object still when in fact it’s working with that remote object?

With a proxy. A proxy is a stand-in for another object that makes the local code think it’s dealing with a local object. Behind the scenes, the proxy connects to the remote object, all the while making the local code believe it’s working with a local object, as you can see in figure below

image

Illustration :problem

Consider a document editor ( Html creator ) that can embed graphical objects in a document. Some graphical objects, like large raster images, can be expensive to create. But opening a document should be fast, so we should avoid creating all the expensive objects at once when the document is opened. This isn’t necessary anyway, because not all of these objects will be visible in the document at the same time.

These constraints would suggest creating each expensive object on demand, which in this case occurs when an image becomes visible.

But what do we put in the document in place of the image? And how can we hide the fact that the image is created on demand so that we don’t complicate the editor’s implementation?

Illustration: solution

  • The solution is to use another object, an image proxy, that acts as a stand-in for the real image.
  • The proxy acts just like the image and takes care of instantiating it when it’s required.

image

  • The image proxy creates the real image only when the document editor asks it to display itself by invoking its Draw operation.
  • The proxy forwards subsequent requests directly to the image.
  • It must therefore keep a reference to the image after creating it.

Illustration: example Document Editor

image

Solution

Design a surrogate, or proxy, object that: instantiates the real object the first time the client makes a request of the proxy, remembers the identity of this real object, and forwards the instigating request to this real object.

Then all subsequent requests are simply forwarded directly to the encapsulated real object.

Applicability

Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Here are several common situations in which the Proxy pattern is applicable:

  1. A remote proxy provides a local representative for an object in a different address space. NEXTSTEP uses the class NXProxy for this purpose.
  2. A virtual proxy creates expensive objects on demand. The Image-Proxy described in the Motivation in an example of such a proxy.
  3. A Protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.
  4. A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.

Typical uses include

  • counting the number of references to the real object so that it can be freed automatically when them are no more references (also called smart pointers).
  • loading a persistent object into memory when it’s first referenced.
  • checking that the real object is locked before it’s accessed to ensure that no other object can change it

Structure

image

Object diagram of a proxy structure at run-time:

image

 

Collaborations

Proxy forward requests to RealSubject when appropriate, depending on the kind of proxy

image

Consequences

The proxy pattern introduces a level of indirection when accessing an object. The additional indirection has many uses, depending on the kind of proxy:

  1. A remote proxy can hide the fact that an object resides in a different address space.
  2. A virtual proxy can perform optimizations such as creating an object on demand.
  3. Both protection proxies and smart references allow additional housekeeping tasks when an object is accessed.

Model

image

1 comment:

dharmendra said...

Great Explanation. Another great article i recommend is this one