Jul 14, 2013

Proxy Design Pattern

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,
  • 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.
In short, the proxy is the object that is being called by the client to access the real object behind the scene. Proxy design pattern gets second rank in popularity in interviews.

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. 
          Lighter part of censor is that, we search for something critical in Google and click the result and we get this page is blocked by proxy server. We never know why this page is blocked and we feel this is genuine. How do we overcome that, over a period you learn to live with it?

  • Smart Reference – Just we keep a link/reference to the real object a kind of pointer.
UML Class Diagram

  • 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.");

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 = new ProxyCar(new Driver(25));

Sorry, the driver is too young to drive.
Car has been driven!

  1. A proxy may hide information about the real object to the client.
  2. A proxy may perform optimization like on demand loading.
  3. A proxy may do additional house-keeping job like audit tasks.
  4. Proxy design pattern is also known as surrogate design pattern.
Compare Proxy with other design patterns?
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.


  1. I really like your this work hope so every one who visit this page too share his comments. Thanks for share this.
    access Bee MP3 in UK

  2. I really like your this work hope so every one who visit this page too share his comments. Thanks for share this.
    access ExtraTorrent in UK

  3. Thanks for giving me information about my problem. I was searching the NET and here I found. Also if you want more work just click
    Mp3Juices UK proxy

  4. That is really nice and easy! My son would enjoy this!
    TorrentCrazy UK proxy