Proxy mode in five-minute Design Mode
Five minutes a design pattern, the simplest way to describe the design pattern.
Understanding proxy Mode
The proxy mode creates a proxy object to represent the real object. After the client obtains the proxy object, it wants to get the same as the real object. The client calls a proxy object to execute the task, but the task is actually the heaviest task by the real object.
The essence of proxy mode is to control object access.
The proxy mode includes the following parts:
Subject: Target Interface
RealSubject: the target object inherited from Subject.
Proxy: the Proxy object inherited from Subject. You can use Proxy instead of RealSubject.
Sample Code
interface Subject{ void Request();}
class RealSubject1:Subject{ public void Request() { Console.WriteLine("request from RealSubject1"); }}
class RealSubject2 : Subject{ public void Request() { Console.WriteLine("request from RealSubject2"); }}
// Proxy class, which implements the same interface as the target object class Proxy: Subject {private Subject realSubject; public Proxy (Subject _ subject) {realSubject = _ subject ;} // The proxy class does not actually execute the task, but calls a specific target object to execute the task. // there is a certain degree of indirect between the client and the specific target object, in this case, the flexibility of the program can be increased. public void Request () {// other method realSubject. request (); // other method }}
Program running result:
Request from RealSubject1
Request from RealSubject2