Proxy mode in five-minute Design Mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.