Click here to Skip to main content
15,892,737 members
Articles / Proxy

Proxy Design Pattern

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Sep 2014CPOL3 min read 10.1K   1   2
Proxy design pattern

Introduction

This is a kind of Structural Design pattern.

A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.

Thus, this enables us to add additional functionality to the object without much tinkering to the existing object.

Some usages which I have encountered are as follows:

  1. To separate Server side code with the Client side. In this case, we will create a Proxy Wrapper to avoid direct contact.
  2. When we want to implement Thread safe functionality to a new/existing functionality.
  3. When we want to add more security to the object.

Let us now look at the components of Proxy pattern:

  1. ISubject: This class provides interface that both actual class and proxy class will implement. Thus the Proxy class will indirectly lead to call of Actual class.
  2. Proxy: This is the main class which will route the call and get the required information for us from the actual class Or real subject.
  3. RealSubject: This is the real object that contains the actual logic to retrieve the data/functionality. This is the class that the proxy represents at application end.

Let us look at a small example to cover this. I have created a small Client / Server code to showcase the same.

First we will look at the ISubject. It has 2 properties.

C#
namespace ProxyDesignPattern
{
   public interface IServer
    {
        string MyName { get; }

        string MyAge { get; }
    }
}

Now we will move towards our Real Subject which implements ISubject as shown below:

C#
class Server : IServer
   {

       public string MyName
       {
           get { return "Aditya"; }
       }

       public string MyAge
       {
           get { return "25"; }
       }
   }

Now we will move towards our Main hero :) The Proxy. As shown below, I have just called the Actual Subject Property to get the data.

C#
public class Proxy
    {
        private IServer _server;

        public string MyName()
        {
            _server = new Server();
          return  _server.MyName;
        }


        public string MyAge()
        {
            _server = new Server();
            return _server.MyAge;
        }
    }

Now the last bit, i.e., the Client.

C#
class Program
   {
       static void Main(string[] args)
       {
           Proxy _proxy = new Proxy();
           Console.WriteLine("My Name is : {0}", _proxy.MyName());

           Console.WriteLine("My Age is : {0}", _proxy.MyAge());
           Console.ReadLine();
       }
   }

Thus the client touches the Proxy and it in turns gets us the data from the server.

A Proxy Design Pattern has various variations that include:

Virtual Proxy

This type of proxy is created to manage creation of an instance of resources that are very expensive in nature, in other words, that consume too much memory or take too much time for accessing the main resource or any other kind of heavy operation.

Remote Proxy

This is similar to the concept of the use of web services or WCF services where the client must access the remote resource on another network, using the proxy class instance.

Protection Proxy

As we explained above, we can create a secure proxy of a resource by adding some kind of authentication for the client code to provide. This is known as a Protection Proxy since the resources are protected by the proxy and the client code must pass the authentication process.

Smart Proxy

This kind of proxy is used to add some kind of functionality to manage the resources in an efficient manner. For example, in our example, we have added the logic that fetches 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.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Passionate about Microsoft Technologies like WPF, Windows Azure, ASP.NET, Win Phone and also on Cross platform Mobile Apps, Mongo DB's, IOT & WOT. I love learning and working with (or) Creating Design Patterns . Writer | Technology Evangelist | Technology Lover | Microsoft Developer Guidance Advisory Council Member | Cisco Champion | Speaker |

Blog : http://adityaswami89.wordpress.com/

Comments and Discussions

 
GeneralOne observation about your Proxy class Pin
John Brett25-Sep-14 2:59
John Brett25-Sep-14 2:59 
GeneralRe: One observation about your Proxy class Pin
adityaswami8930-Sep-14 1:00
professionaladityaswami8930-Sep-14 1:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.