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:
- To separate Server side code with the Client side. In this case, we will create a Proxy Wrapper to avoid direct contact.
- When we want to implement Thread safe functionality to a new/existing functionality.
- When we want to add more security to the object.
Let us now look at the components of Proxy pattern:
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. 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. 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.
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:
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.
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.
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.