Proxy Design Pattern(代理设计模式)

转自:http://www.codeproject.com/Articles/186001/Proxy-Design-Pattern

 

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值