转自: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 theProxy
class and theRealSubject
class implements.- It has the
Operation
method.
- It has the
- The
Proxy
class is the proxy for theRealSubject
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'sOperation
method. This is the place where you can add additional logic to the real object's methods.
- It has the
- The
RealSubject
class is the real object behind the scene.- It has the
Operation
method that performs the actual work.
- It has the
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 theProxyCar
class and theCar
interface implements.- It has the
MoveCar
method.
- It has the
- The
ProxyCar
class is the proxy for theCar
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'sMoveCar
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'sMoveCar
method.
- It has the
- 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)