Click here to Skip to main content
Click here to Skip to main content

Proxy Design Pattern

, 4 May 2011
Rate this:
Please Sign up or sign in to vote.
Proxy Design Pattern clearly explained with example

You can see this and other great articles on design patterns here.

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.

Let's look at the UML of the proxy pattern first, then we will look at an example to see how it works. Below is the UML of the proxy design pattern:

  • The ISubject interface is the interface that both the Proxy class and the RealSubject class implements.
    • It has the Operation 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 Operation method that encapsulate the real object's Operation 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 Operation method that performs the actual work.

Let's see an example. In our example, we have household members and cars, and these are the existing classes that we cannot change. We want to further specify that only household members that are over 16 years old can drive cars, and we will add this logic into the proxy class. Below is the UML of our example:

  • The ICar interface is the interface that both the ProxyCar class and the Car interface implements.
    • It has the MoveCar method.
  • The ProxyCar class is the proxy for the Car class.
    • It has the realCar variable that points to the real object behind the scene.
    • It has the MoveCar method that encapsulates the real object's MoveCar method. This is the place where we put in the additional logic to determine if the driver is over 16 years old. If the driver is not over 16 years old, it will not call the real object's MoveCar method.
  • The Car class is the real object behind the scene.

 

Below are the implementation code and the output of the proxy pattern using our 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:

class Program
{
    static void Main(string[] args)
    {
        ICar car = new ProxyCar(new Driver(16));
        car.MoveCar();

        car = new ProxyCar(new Driver(25));
        car.MoveCar();
    }
}

interface ICar
{
    void MoveCar();
}

//the proxy
class ProxyCar : ICar
{
    private Driver driver;
    private ICar realCar;

    public ProxyCar(Driver driver)
    {
        this.driver = driver;
        realCar = new Car();
    }

    void ICar.MoveCar()
    {
        if (driver.Age <= 16)
            Console.WriteLine("Sorry the driver is too young to drive");
        else
            realCar.MoveCar();
    }
}

//the real object
class Car : ICar
{
    void ICar.MoveCar()
    {
        Console.WriteLine("Car has been driven");
    }
}

class Driver
{
    private int age;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public Driver(int age)
    {
        this.age = age;
    }
}

Liked this article? You can see this and other great articles on design patterns here.

License

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

About the Author

DevLake

United States United States
No Biography provided

Comments and Discussions

 
QuestionConnect my app with an External API PinmemberJuanSilva19-Mar-14 10:08 
GeneralMy vote of 5 Pinmembergabrielnascimento14-Sep-13 12:09 
GeneralMy vote of 4 PinmemberMember 869188613-Mar-13 6:15 
GeneralMy vote of 5 PinmemberMember 914438017-Jan-13 15:59 
QuestionProxy Pattern is not Class Proxy PinmemberEthan Woo21-Jul-11 15:26 
GeneralMy vote of 5 PinmemberOshtri Deka11-May-11 1:06 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid