Proxy design pattern of C # design pattern (proxy)

Source: Internet
Author: User
Tags firewall

Proxy mode provides a proxy for an object and controls a reference to the original object by the proxy object.

Agent mode of English is called proxy or surrogate, Chinese can be translated into "agent." An agent is a person or an agency acting on behalf of another person or another institution. In some cases, a customer does not want or cannot directly refer to an object, and the proxy object can act as a mediator between the client and the target object.

Second, the type of agent

If divided according to the purpose of use, the agent has the following:

Remote proxy: Provides a local-domain representation object for an object located in a different address space. This different address space can be in this machine, but also in another machine. The remote agent is also called the Ambassador (Ambassador).

Virtual Agent: Create a resource-consuming object as needed, so that the object is actually created only when it is needed.

Copy-on-write agent: A kind of virtual agent. Delay replication (cloning) until the client needs it to take action.

Protection (Protect or access) Proxy: Controls access to an object and, if necessary, provides different levels of use to different users.

Cache Agent: Provides temporary storage space for the results of a target operation so that multiple clients can share the results.

Firewall (Firewall) Agent: Protect the target, not to allow malicious users to approach.

Synchronization (synchronization) Proxy: Enables several users to use an object at the same time without conflict.

Smart Reference Proxy: When an object is referenced, provide some extra action, such as logging the number of calls to this object.

In all kinds of proxy modes, virtual agents, remote proxies, smart reference proxies (smart Reference proxy), and protection (Protect or Access) proxies are the most common proxy patterns.

Iii. Examples of remote proxies

Achilles is a tool software used to test the security performance of a Web site. Achilles is equivalent to a desktop proxy server located on the client, which acts as a middleman in an HTTP process, but Achilles differs from the usual proxy server. Achilles intercepts two-way communication data, allowing users of Achilles software to change data from and to a network server, or even to intercept and modify SSL communications. (This is not very clear in Java and schema, for the encryption of asymmetric key blocking, cracking methods, you can refer to my other article, "Intercept and modify asymmetric key encryption information through the agent").

Another example is a shortcut to Windows. A shortcut is a proxy for the program it references.

IV. Structure of Agent mode

The class diagram for proxy mode is shown in the following illustration:

The roles involved in agent mode are:

Abstract theme Role (Subject): Declares a common interface between real and agent topics, so you can use proxy topics wherever you use real topics.

Agent theme (proxy) role: The agent theme role contains a reference to a true subject, so that the real subject object can be manipulated at any time, and the agent theme role provides an interface that is the same as the real theme role, so that it can be substituted for the real subject at all times, Responsible for creating real subject objects (and deleting real subject objects) when needed; Agent roles usually perform an action before or after the client call is passed to the real topic, rather than simply passing the call to the real subject object.

Real-World theme Role (realsubject) role: Defines the real object represented by the proxy role.

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.