Sunday, 6 November 2011

Proxy Pattern

A Proxy design pattern is a structural design pattern as it defines a manner for creating relationships between classes or entities.

The proxy design pattern is used to provide a surrogate or placeholder object as a representative for the communication of the clients of a component. Introducing such a placeholder can serve many purposes, including enhanced efficiency, easier access and protection from unauthorized access.

A brief documentation of this design pattern proxy is given below.

Name: Proxy

Context:

A client needs access to services of another component. Direct access is possible, but it may not be the best approach. For example: A check or bank draft is a proxy for funds in an account. A check can be used in place of cash for making purchases and ultimately controls access to cash in the issuer’s account.

Problem:

It is not appropriate to access a component directly. This hinders the protection from unauthorized access and we do not want to instantiate such objects unless and until they are actually requested by the client.

Solution:

In order to ensure protection from the unauthorized access to the components, the client is made to communicate with the representative or the placeholder instead of communicating directly with the component itself. This representative is called as the “proxy”. The proxy offers the interface for component and also performs the additional functions such as access-control and checking or making the read only copies of the original content.

Structure:

The structure that has been shown below clearly shows that, by defining a Subject interface, the presence of the Proxy object standing in place of the Real Subject is transparent to the client.



Original or real subject: This could be any particular service like actions such as returning or displaying data or data retrieval or computations involving other components.

Client: The client has a specific task and to accomplish this task, the client invokes the functionality of the original subject indirectly by accessing the proxy.

Proxy: This provide interface as the original subject to ensure correct access to the original. It also maintains a reference to the original content that it represents.

Subject or abstract subject: It provides the interface implemented by the proxy and the original subject.

Implementation:

· Firstly identify the responsibilities for dealing with access control to a component, then assign these responsibilities to the separate component known as the “proxy”.

· Introduce an abstract base class that specifies the common parts of the interfaces of both the proxy and the original, then derive the proxy and the original from this base class.

· Implement the functions of the proxy.

· Free the original and its clients from responsibilities that have migrated into the proxy.

· Associate the proxy and the original by giving the proxy a handle to the original. This handle could be a pointer, a reference, an address etc..

· Finally remove all direct relationships between the original and its clients and then replace them by analogous relationships to the proxy.

Variants:

The design pattern proxy consists of variants such as the Remote proxy Protection proxy, Cache proxy, Synchronization proxy, Counting proxy, Virtual proxy, Firewall proxy etc..

Benefits:

· Enhanced efficiency and cost efficient.

· There is a decoupling of clients from the location of server components.

· Separation of house keeping code from functionality.


No comments:

Post a Comment