Design Patterns Simplified - Part 13 (Proxy)

I am here to continue the discussion around Design Patterns. Today, we will go through one of the structural design patterns called Proxy.

Also in case you have not had a look at our previous articles, go through the following links:

Before talking about its implementation let’s begin with defining it.

As per GOF guys, Proxy pattern is defined as follows.

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

Well! Let’s understand what they mean and where this pattern can be fit into.

As the name suggests, proxy pattern is used to provide proxy or surrogate object in order to access actual object. Surrogate object typically sits in local machine and mimics the actual object which may be part of another application or domain or sitting in remote servers.

By having surrogate object in a local machine, the implementation details to connect to actual objects are abstracted and that lead to clean design.

Remember creating service references or web references to connect to WCF or Web Service? This also creates proxy objects behind the scene and mimics to client as original object.

Now let’s see the UML of proxy pattern.

pattern

Proxy pattern has three key elements as following.

  • Subject: This is an interface which will be implemented by both RealSubject and Proxy.

  • RealSubject: This is an actual class that implements Subject interface members.

  • Proxy: This is class which sits in local machine to mimic the real object (i.e. RealSubject). It keeps the reference of RealSubject in order to access its members.

How Proxy pattern works?

We will understand this by simple example.

Let’s take an example of a stock price web service. There are lot of paid and free web services available today to provide stock quotes. Here we will try to mimic the same.

Let’s start with creating Subject interface as following.

  1. /// <summary>  
  2.     /// The Subject interface  
  3.     /// </summary>  
  4.     public interface IStockPrice  
  5.     {  
  6.         double GetCurrentPriceByStockId(string UniqueId);  
  7.     }  
Now create Actual class or RealSubject that implements Subject interface (IStockPrice).
  1. /// <summary>  
  2.     /// The RealSubject class  
  3.     /// </summary>  
  4.     public class StockPrice : IStockPrice  
  5.     {  
  6.         public double GetCurrentPriceByStockId(string UniqueId)  
  7.         {  
  8.             return 1206.20;  
  9.         }  
  10.     }  
In real word, the Subject Interface and RealSubject class won’t be available in client’s machine and only proxy class would sit in client or local machine.

Now let’s define proxy class.
  1. /// <summary>  
  2.     /// The Proxy class  
  3.     /// </summary>  
  4.     public class StockPriceProxy: IStockPrice  
  5.     {  
  6.         IStockPrice stockPrice;  
  7.         public double GetCurrentPriceByStockId(string stockId)  
  8.         {  
  9.             stockPrice = new StockPrice();  
  10.             return stockPrice.GetCurrentPriceByStockId(stockId);  
  11.         }  
  12.     }  
Setup is ready now. Let’s see how client uses it.
  1. Console.Title = "Proxy Pattern demo";  
  2.   
  3.             IStockPrice price = new StockPriceProxy();  
  4.             string brandStockId = "INFY";  
  5.             Console.WriteLine("Current Stock Price for Brand {0} is: {1}", brandStockId, price.GetCurrentPriceByStockId(brandStockId));  
Output:

output

So you can see that client calls the proxy class method and now it’s the responsibility of proxy class to route the request to actual class and return the result back to client.

Also there are different flavors of proxies. Some of the commonly used are as follows: 
  • Virtual: Provides cached or default result to start with if real object calls is expensive.

  • Remote: Encodes requests and its arguments and send them to real subject. Ex: WCF proxy.

  • Protection/Authentication: Checks if the caller has required access permissions for a request to fulfill.

Important note:

Sometimes people get confused with the purpose of proxy pattern with Decorator and Adapter. Please note that purpose of Decorator is to add additional functionalities on an object dynamically and Adapter pattern is used to provide a changed interface for the object to solve incompatibility issues whereas Proxy uses the same interface used in actual class but abstracts the implementation details from the client.

Hope you have liked the article. Look forward for your comments/suggestions.

Read more articles on Design Patterns:

Up Next
    Ebook Download
    View all
    Learn
    View all