Design Patterns Learning Notes (19: Proxy mode)

Source: Internet
Author: User

1.1 Overview

provides a proxy for other objects to control access to this object. This is the definition of proxy mode.

When a user wants to interact with an object, but the program may not want the user to access the object directly, it provides a special object, called the current user's proxy to access the object, which allows the user to interact with the object's proxy, which allows the user to access the object they want to access through the Access Proxy. In proxy mode, the agent is characterized by that it implements the same interface as the object being proxied, that is, the proxy and its multi-agent object expose the same method to the user, and when the user requests the proxy to invoke the method, the agent may need to verify some information or check that the object it proxies is available. When the agent confirms that the object it is acting on can invoke the method of the box ventilation, it delegates the actual method call to the object it proxies, that is, to have the proxy object invoke the same method.

For example, the company's secretary is the boss's agent, the boss and the Secretary have a way to listen to the phone:herephone (). The company requires the user must first and the Secretary on the phone to speak with the boss, that is, the user must first request the Secretary to call Herephone (), when the secretary to confirm that the boss can answer the phone, the user's actual request to the boss, that is, let the boss call herephone () method. The specific class relationship is as follows:

Figure I: Boss and Secretary

Structure of the 1.2 pattern

The proxy mode consists of the following three roles:

(1) abstract theme (Subject): An abstract topic is an interface that is the interface that the object and its agents share, that is, the realsubject role and proxy the interface implemented by the role instance.

(2) actual theme (realsubject): The actual theme is the class that implements the abstract topic interface. An instance of a real topic is anobject to be proxied by an instance of an agent role (proxy).

(3) proxy: Theproxy is the class that implements the abstract topic interface (the proxy and the actual topic implement the same interface). The proxy has a variable that contains the topic interface declaration, which is used to hold an instance reference to the realsubject role, so that an instance of the agent can control the realsubject It contains Instance access to the role, that is, you can control access to the object it proxies.

The class diagram for the proxy schema structure is shown in two:

Figure two: Class diagram of the proxy mode

1.3 Agent advantages of the model

(1) The proxy mode can block the object that the user really requests, and decouple the user program from the real object.

(2) use proxies to act as aliases for those who create time-consuming objects.

1.4 Fit using proxies pattern of the scene

(1) The program may not want the user to access the object directly, but instead provides a special object to control access to the current object.

(2) If an object (such as a large image) takes a long time to load complete.

(3) If the object is on a remote host, you need to provide the user with the ability to access the remote object.

Design Mode Learning Note (19: proxy mode)

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.