The proxy design pattern allows you to provide an interface to other objects by creating a wrapper class as the proxy. The wrapper class, which is the proxy, can add additional functionality to the object of interest without changing the object's code.
Below are some of the common examples in which the proxy pattern is used,
Guess who gets the first rank? None other than singleton design pattern.
Possible Usage Scenarios
Below are the implementation code and the output of the proxy pattern using example. Notice that the proxy (ProxyCar) provided a gateway to the real object (Car) and provided additional functionality without changing the real object's code.
Real Object
Proxy Object
How to use above Proxy class?
Output
Notes:
Adapter vs Proxy: Adapter design pattern provides a different interface from the real object and enables the client to use it to interact with the real object. But, proxy design pattern provides the same interface as in the real object.
Decorator vs Proxy:Decorator design pattern adds behavior at runtime to the real object. But, Proxy does not change the behavior instead it controls the behavior.
Below are some of the common examples in which the proxy pattern is used,
- Adding security access to an existing object. The proxy will determine if the client can access the object of interest.
- Simplifying the API of complex objects. The proxy can provide a simple API so that the client code does not have to deal with the complexity of the object of interest.
- Providing interface for remote resources, such as web service or REST resources.
- Coordinating expensive operations on remote resources by asking the remote resources to start the operation as soon as possible before accessing the resources.
- Adding a thread-safe feature to an existing class without changing the existing class's code.
Guess who gets the first rank? None other than singleton design pattern.
Possible Usage Scenarios
- Remote Proxy– Represents an object locally which belongs to a different address space. Think of an ATM implementation, it will hold proxy objects for bank information that exists in the remote server.
- Virtual Proxy– In place of a complex or heavy object, use a skeleton representation. When an underlying image is huge in size, just represent it using a virtual proxy object and on demand load the real object. You feel that the real object is expensive in terms of instantiation and so without the real need we are not going to use the real object. Until the need arises we will use the virtual proxy.
- Protection Proxy– Are you working on a MNC? If so, we might be well aware of the proxy server that provides us internet by restricting access to some sort of websites like public e-mail, social networking, data storage etc. The management feels that, it is better to block some content and provide only work related web pages. Proxy server does that job. This is a type of proxy design pattern.
- Smart Reference– Just we keep a link/reference to the real object a kind of pointer.
- The Subject is the interface that both the Proxy class and the RealSubject class implements.
- It has the Request method.
- The Proxy class is the proxy for the RealSubject class.
- It has the realSubject variable that points to the real object behind the scene.
- It has the Request method that encapsulates the real object's Request method. This is the place where you can add additional logic to the real object's methods.
- The RealSubject class is the real object behind the scene.
- It has the Request method that performs the actual work.
Below are the implementation code and the output of the proxy pattern using example. Notice that the proxy (ProxyCar) provided a gateway to the real object (Car) and provided additional functionality without changing the real object's code.
interface ICar
{
void DriveCar();
}
Real Object
public class Car : ICar
{
public void DriveCar()
{
Console.WriteLine("Car has been driven!");
}
}
Proxy Object
public class ProxyCar : ICar
{
private Driver driver;
private ICar realCar;
public ProxyCar(Driver driver)
{
this.driver = driver;
realCar = new Car();
}
void ICar.DriveCar()
{
if (driver.Age <= 16)
Console.WriteLine("Sorry, the driver is too young to drive.");
else
realCar.DriveCar();
}
}
public class Driver
{
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
public Driver(int age)
{
_age = age;
}
}
How to use above Proxy class?
private void btnProxy_Click(object sender, EventArgs e)
{
ICar car = new ProxyCar(new Driver(16));
car.DriveCar();
car = new ProxyCar(new Driver(25));
car.DriveCar();
}
Output
Sorry, the driver is too young to drive.
Car has been driven!
- A proxy may hide information about the real object to the client.
- A proxy may perform optimization like on demand loading.
- A proxy may do additional house-keeping job like audit tasks.
- Proxy design pattern is also known as surrogate design pattern.
Adapter vs Proxy: Adapter design pattern provides a different interface from the real object and enables the client to use it to interact with the real object. But, proxy design pattern provides the same interface as in the real object.
Decorator vs Proxy:Decorator design pattern adds behavior at runtime to the real object. But, Proxy does not change the behavior instead it controls the behavior.