Proxy design pattern using C#


In this article, we will be discussing the Proxy pattern. It is the part of structural design patterns. As the name suggests, it will act as representation of an object, and will basically be the access point to use the original object. This kind of situation is possible when we are having resources that are may or may not be in the same code or machine or even network. This is similar to the concept of using the web services or WCF services to access the required resources.

For an example, we may have a web service API to get the live stock market feed or any live cricket match score. Client applications which need these kind of data, cannot directly access the resources and have to use some kind of intermediate to fetch it. In this case, any API service acts as a Proxy on behalf of the actual resource. This kind of pattern allows not only to control the access to the resources only when required, but also provides security as the client code cannot directly access the resource and must pass through the proxy.

This pattern may sound similar to the adapter, mediator or bridge patterns but it is not. All these patterns play same kind of role by being an intermediate, but the main aim or the reason for which they are designed, distinguish them from one another. We will discuss briefly about the difference between these patterns, at the end of this discussion.

What is Proxy pattern ?

According to GoF’s definition, this pattern is defined as :

Provide a surrogate or placeholder for another object to control access to it. 

A real world example :

Let’s say that there is a third party API to get the Gold prices in the stock market. These prices are not available for the client application directly. Client application must use a proxy class to get the actual resources, as the main system, which provides this kind of data, cannot allow any third party to access its database directly. The main point here will be that, the proxy will fetch the current prices from the actual source, only if the client says yes or no. So our client code will be calling the proxy class, proxy class will get the price from the actual source and send the current price, back to the client code.

Let’s convert the above real world example into technical code:

We will be dividing the system into the following components for our implementation of this pattern :

1. ISubject : This is the simple interface used to define the abstract resources that we want to access using the proxy class or you can say it contains the abstract functions that we want to share with the client code though the proxy class. So in our case, it will be IPrice with a function to return the prices of Gold, using the GoldPrices class, which will implement this interface.

2. Subject : This is the implementation of the above interface or the concrete class. As discussed above, in our case, it will be GoldPrices class, which will implement the above interface.

3. Proxy Class: This is the main class of the system, used by the client code to access the resources. This proxy class will contain the reference to the subject classes, in the form of the interface of the subject.  

In our example, it will be the ProxyAPI class. It will be having a reference of IPrice interface. This reference will be further used to access the functions of the subject class or GoldPrices class indirectly.

So our initial code for the above structure will be :

Proxy_1

Now we have to write our client code, which will be simply creating a proxy class instance and call its function. This function will then internally call the actual function of the resources, based on the input from the client code. Finally it will return the output to the client code. So our code will be like the following :

Proxy_2

So as we can see here that it is the proxy which is calling the actual resource. Different kind of business logic like whether the call is from authenticated source or not, or any other logic can be added to the proxy layer and control the way the resource is being accessed.

Now, at the start of the discussion, we mentioned about different patterns, that may sound similar to this pattern but are meant to achieve different functionality. So let’s discuss them briefly here :

Adapter vs Mediator vs Bridge vs Proxy patterns :

Adapter pattern is aimed at solving the in-compatibility issues between two interfaces or we can say its like a language converter concept, converting one language sentence to another.

Bridge pattern is aimed at providing different implementations for a task and and each of these implementations can be used in different ways. For ex, send an SMS or email notification to user and send them in different ways like using web-service or third party tools.

Mediator pattern is aimed at managing the complexity of the system by handling how the components or classes of the system, interact with each other. Otherwise, each of these classes will be having references to the other classes of the system, in order to call each other. This would result in a spider web type system classes.

Proxy pattern again, acts as main point of contact for the resources that should not be available directly to the client code.

Proxy pattern is having different variations which include :

1. Virtual Proxy : This type of proxy is created to manage creation of instance of resources which are very expensive in nature i.e. consume too much memory or take too much time for accessing the main resource or any other kind of heavy operation.

2. Remote proxy : This is similar to the concept of the use of Web-services or WCF services where the client need to access the remote resource, which is on another network, using the proxy class instance.

3.  Protection proxy : Like we discussed above, we can create a secure proxy of a resource, by adding some kind of authentication for the client code to provide. This is know as Protection proxy as the resources is protected by the proxy and client code must pass the authentication process.

4. Smart Proxy : This kind of proxy is used to add some kind of functionality in order to manage the resources in efficient manner. For ex, in our example, we have added the logic that fetch the resource, only if it is required by the client code. In this way, you can add some kind of singleton pattern so that only one instance of the subject is created or so on.

So depending on our requirements, we may have combination of one or more of these variations like protection proxy can be added along with the virtual and remote proxy to authenticate the client making request for the required data. This was all about Proxy pattern. Hope you enjoyed reading it.

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in Design Patterns. Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s