Design Mode (11) --- proxy mode, design mode 11 --- proxy

Source: Internet
Author: User

Design Mode (11) --- proxy mode, design mode 11 --- proxy

I. digress

This section describes the proxy mode. I personally feel that the proxy mode is very simple and there is almost no difficulty in using it. Therefore, let's simply describe it.

 

Ii. Definition

Proxy mode: provides a proxy for other objects to control access to this object.

Explanation: for example, if you want to buy an iPhone 6 in the United States but you are not in the United States, it happens that you have a friend on a business trip to the United States, so you ask a friend to help buy an iPhone 6 on behalf of you. The whole process is the proxy mode, you are the target object, and your friends act as agents to complete the operations you want on your behalf.

 

Iii. UML class diagram and basic code

Basic code:

class Program    {        static void Main(string[] args)        {            Proxy proxy = new Proxy();            proxy.Request();            Console.Read();        }    }    abstract class Subject    {        public abstract void Request();    }    class RealSubject : Subject    {        public override void Request()        {            Console.WriteLine("real request");        }    }    class Proxy : Subject    {        RealSubject realSubject;        public override void Request()        {            if (realSubject == null)            {                realSubject = new RealSubject();            }            realSubject.Request();        }    }

 

Iv. Advantages and Disadvantages and application scenarios:

Advantages:

1) The proxy mode can separate the proxy object from the called object, reducing the Coupling Degree of the system to a certain extent.

2) The proxy mode acts as an intermediary between the client and the target object, which can protect the target object. The proxy object can also perform other operations before calling the target object.

 

Disadvantages:

1) adding a proxy object to the client and target object slows down request processing.

2) increases the complexity of the system.

 

Use Cases:

1) remote proxy provides a local representation of an object in different address spaces. In this way, the fact that an object exists in different address spaces can be hidden.

2) virtual Proxy: Creates objects with high overhead as needed. It stores objects that take a long time for instantiation.

3) Security Proxy, used to control the access permissions of real objects.

4) intelligent guidance: when a target object is called, the proxy can handle other operations.

Related Article

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.