Proxy mode

xiaoxiao2021-03-06  68

Summary

This article discusses the concept, kindness, strengths, and shortcomings of proxy models, as well as implementation in Visual Basic.NET and C # languages ​​(see appendix), and relationship between agent mode and other design modes.

table of Contents

Proxy mode concept

Agent type

Example of a remote agent: Achilles

Shortcut: Agent's example

Proxy mode structure

Demographic timing

Length and shortcomings of proxy model

Mode implementation

Proxy mode relationship with other modes

Appendix, C # code

references

Proxy mode concept

The proxy model is the structure mode of the object [gof95]. The proxy mode provides a proxy object to an object and controls the reference to the original object by the proxy object.

English is called proxy or surrogate, and Chinese can be translated into "proxy". The so-called agent is a person or a body represents another person or another institution. In some cases, a customer does not want or cannot directly reference an object, and the agent object can play a role between the client and the target object.

Agent type

If you are divided according to your purpose, the agent has the following:

Remote Agent provides a local representative object for an object in different address spaces. This different address space can be in this machine, or in another machine. The remote agent is also called Ambassador.

Virtual (Virtual), create a larger-consuming object, which is created as needed, so that this object is only true when needed. This chapter gives an example of a load image to illustrate the use of the virtual agent.

A Copy-on-Write Agent, a virtual agent. Take actions when you delay the replication (clone) to only when you need it.

Protect or Access agent controls access to an object, if you need to provide different levels of use permissions to different users.

The Cache agent provides temporary storage space for the results of a target operation so that multiple clients can share these results.

Firewall agent, protective goals, do not allow malicious users close.

Synchronization proxy makes several users to use an object simultaneously without conflict.

Smart Reference Agent When an object is referenced, some additional operations are provided, such as the number of times the object calls will be recorded.

In all types of proxy mode, virtual (Virtual) agent, Smart Reference Proxy and Protection (Protect Or Access) is the most common type of proxy mode.

In some books that explain the design mode (such as [Grand98]), different proxy models are divided into independently, to emphasize their different; all agent patterns are placed in some explanations, to emphasize their Common. This paper takes the explanation of [YAN02], first put all the proxy models into a local explanation, and then provide different examples for different types, and explain the implementation method.

Example of a remote agent: Achilles

Achilles is a tool software for testing the security performance of the website. Achilles is equivalent to a desktop proxy server located on the client, playing a middleman in an HTTP process, but Achilles is different from the usual proxy server.

A usual HTTP proxy software forwards a client's HTTP packet to the web server. Achilles intercepts two-way communication data such that users of the Achilles software can change data from and sent to web servers. For example, in a normal SSL connection, a usual proxy server will forward communication such that both parties can discuss SSL connections; and Achilles is different. When Achilles is intercepted, it prefers to the client as a server, and the real server is pretending to be a browser, discusing SSL communication at both ends. Achilles can crack the encrypted data, display the user of the user that has decrypted, and allows the user to change the data in the communication process. The following shows that Achilles software is running at runtime:

Figure 1, Achilles runs.

The reader can download this software for free from http://www.digizen-security.com/projects.html.

Obviously, for the browser, Achilles agents are a remote network server. Achilles work mode is the application of remote proxy mode.

Shortcut: Agent's example

The Windows system provides shortcuts that allow any objects to appear in multiple places simultaneously without having to modify the original object. The call to the shortcut is completely like the call to the original object, in other words, the shortcut is completely transparent to the client.

Figure 2, the shortcuts of the service.

For example, the icon above is a proxy for Windows services. All shortcuts have a small arrow at the bottom left of the icon. That is to say, the user can distinguish the original object and the agent to the original object. If the original object is deleted, the shortcut can still be present, but an error will be given during the call.

In the figure below, a shortcut called LINK1 is a proxy with files named Explorer.exe. When the user launches this shortcut, Link1 passes the user's call to the Explorer.exe file it proxy.

Figure 3, an example of a shortcut.

There is Alias ​​in Macintosh, there is a link in UNIX, which is the application of the proxy mode as a convenient way of Windows.

Proxy mode structure

The roles involved in the proxy model are:

Abstract theme characters, declare the common interface of the true subject and the agency theme, so that any agent theme can be used in any place where you can use the real theme.

Proxy Topics (Proxy) role, first-agent theme role, including reference to real topics, so that you can operate real-topic objects at any time;

Secondly, the agency theme character provides an interface as the true subject character, so that you can replace the true subject at any time;

Third, control the reference to the real topic, responsible for creating a true subject object (and delete real subject object) when needed;

Fourth, the agent role usually performs an action before or after transferring the client to the true topic, rather than simply transmitting the call to the true subject object.

The real theme role defines the true objects represented by the agent role.

Let's give a very simple schematic implementation, please see the category of implementation:

Figure 4, the structural diagram of the proxy mode.

The following is a schematic source code for the abstract topic role, it can be seen that this role specifies that all the subject objects must implement the request () method:

Public Mustinherit Class SUBJECT

Public Mustoverride Sub Request ()

END CLASS

Code List 1, source code for the subject character.

Below is a schematic source code for the specific theme role. Here only gives a schematic implementation of the request () method:

Public Class RealSubject

Inherits Subject

Public SUB RealSubject ()

System.Console.writeline ("RealSubject Object Is Created.")

End Sub

Public overrides sub request ()

System.console.writeline ("RealSubject.Request ().")

End Sub

END CLASS

Code Listing 2, the source code of the real theme role.

Below is the source code for proxy agencies. It can be seen that the agency theme is delegated to the true atrial role in addition to the original request, and then executes a prerequest () operation and a postRequest () operation before delegate.

Public Class ProxySubject

Inherits Subject

Private RS As RealSubject

Public Sub New ()

RS = new realsubject ()

End Sub

Private sub prerequest ()

System.Console.writeline ("Before Passing Request to RealSubject.")

End Sub

Private sub posrequest ()

System.Console.writeline ("After Passing Request to RealSubject.")

End Sub

Public overrides sub request ()

Prerequest ()

rs.Request ()

PostRequest ()

End Sub

END CLASS

Code List 3, the source code for the agency topic.

When using a proxy topic, be careful that the significant type of variable is declared as the type of abstract body, and set its real type to a proxy topic type. See the example below:

Private Subj As Subject

.

Subj = new proxySubject ()

Subj.Request ()

Code Listing 4, how to call a proxy topic.

At runtime, the following information is printed:

BEFORE Passing Request to RealSubject.

Realsubject.Request ().

After Passing Request to RealSubject.

Code List 5, how to call a proxy topic.

The schematic source code from the proxy topic class can see how the agent mode works. First, the agency theme does not change the subject's interface, because the mode of mode is not allowed to feel the existence of the agent; secondly, the agent use delegation will appoint the client's call to the true topic object, in other words, the agency theme is A switching request; third, agency topic can perform a specific operation before and after delivery request, not a simple delivery request.

Demographic timing

The class diagram is static and is not suitable for reflecting the characteristics of the mode at runtime; the timing diagram can reflect the activity of the mode. Here is the timing chart of the proxy mode discussed:

Figure 5, timing chart of the agent mode.

As can be seen from the above timing chart, the client issues a request to the agent topic, and the agency topic executes a prerequest () operation while receiving the request, and then pass the request to the real topic. After returning the request returns the request, the proxy topic has executed a PostRequest () operation, returns the control to the client. See the object map below.

Figure 6. Object diagram of the proxy mode.

Compared to the case where the client issues a request to the real topic, a significant benefit of using the agency theme is that the system provides the control of the client request to the real topic. The agency theme can perform a specific operation before passing the client request to the real topic, and decides whether to pass the request to the real topic; the agency topic can perform another operation after passing the client request to the real topic, such as the client Request count, etc. It is possible that the client does not directly file a request to the real topic, and the agency topic has this license, so the agency topic can determine whether to transfer requests after executing prerequest () operations, and so on. In summary, the proxy mode is inserted between a middle layer into the client and the subject role, providing a lot of flexibility. Length and shortcomings of proxy model

There are different characteristics depending on the agent's type agency model.

Remote agent

The advantage is that the system can hide the details of the network so that the client does not have to consider the existence of the network. Customers can consider the objects of the agent to be a local area rather than a remote, and the agent object has undertaken most of the network communication work.

Figure 7, the structural diagram of the remote agent.

Since the customer may not realize that the time-consuming remote call is started, the customer does not have the necessary ideas.

Virtual agent

The advantage of using the virtual proxy model is that the agent object can be loaded by the agent when necessary; the agent can add the necessary optimization to the loaded process. When the loading of a module is very resource, the advantage of the virtual agent is very obvious.

A detailed example is given in the following sections, and the reader can learn from the virtual agent implementation.

Protection agent

The benefit of the protection agent is that it can check the user's related permissions at runtime, and then decide to pass the call to the objective object after verification.

Intelligent reference agent

Some internal service processing (Housekeeping) can be performed when accessing an object, such as counting operations.

An example will be given in the following sections for readers to learn the implementation details of intelligent reference agents.

Mode implementation

The proxy model may not know the true subject object, but only hold an interface of a proxy object. At this time, the agent object cannot be created by the proxy object, and the object object must have a system in which the system is created and incorporated. In fact, this approach can provide greater flexibility.

Java 2.0 provides support for proxy models to take this approach, see the "Java 2.0 Support for Agent Mode" in this chapter.

Proxy mode relationship with other modes

Proxy mode is related to other modes such as transformer mode.

Transformer mode

Rough looks, the transformer mode is very similar to the proxy model, which is considered to provide a pre-interface. However, the intention of the transformer mode is to change the interface of the considered object; and the agent mode cannot change the interface of the agent's object, and there is a significant difference in this two modes.

Figure 8, a brief class of transformer mode. The left is the transformer mode of the class, and the right side is the transformer mode of the object.

Decorative mode

Decorative mode has the same interface as the decorated object, so both modes may be confused. However, the decorative mode should provide enhancements for the object being decorated; and the agent mode is applied to the use of the object, does not provide the enhancement function of the object itself.

Figure 9, a brief class of decorative mode.

Facade pattern

Sometimes the facade model is responsible for the agent. The facade object may be a remote agent of a subsystem located in another address space; sometimes the facade pattern is a role of the protector, check the caller's permissions; sometimes the interface mode is responsible for recording the number of subsystems called, so The role of intelligent reference agents; sometimes the facade pattern is served as a virtual agent role, especially when the subsystem is loaded and the time is time and resources.

This time, the facade pattern is called the proxy facade mode, or the facade agent mode.

Figure 10, a brief class diagram of the facade pattern.

Appendix, C # code

The C # language version of all the code in this article will be given below. First, the code list 1-3 corresponds to

Using system;

Namespace proxy {

Public interface isubject {

Void Request ();

}

Public class proxySubject: isubject {

Private realsubject _real

Public proxySubject () {

_real = new realsubject ();

}

PUBLIC VOID Request () {

Preprocess ();

_real.Request ();

PostProcess ();

}

Private void preprocess () {

System.Console.writeline ("Before Handling Request.");

}

PRIVATE VOID PostProcess () {

System.Console.writeline ("After Handling Request.");

}

}

Public class realsubject: isubject {

Public realsubject () {

System.Console.writeline ("RealSubject is instantiated.");

}

Public void request () {

System.console.writeline ("RealSubject.Request ());

}

}

}

And the code list 4 corresponds to:

Private proxySubject proxy;

. . .

Proxy = new proxySubject ();

Proxy.Request ();

references

[Gof95] E.Gamma, R. Helm, R. Johnson, And J. Vlissides, Design Patterns - Elements of Reusable Object-Oriented Software, 1995, Addison-Wesley.

[Grand98] Mark Grand, Patterns in Java, Vol. 1, John Wiley & Sons, Inc., 1998.

[Yan02] Hong, Java and Model, China Electronic Industry Press, October 1, 2002

About the Author

Hong, born in Tianjin in 1964. In 1987, he graduated from the Modern Physics Department of China University of Science and Technology. In 1990, he was a master's degree in the Chinese Academy of Sciences. In 1992, he won his Ph.D. in Japan to Kyoto University in Japan.

The author has served in the US Citibank, the US Tomson Financial (Thomson Financial), the US Oppenheimer, etc., has conducted many years of software development, architecture design and technical management. He is currently a technical directory of China Blue Data Company.

Copyright Notice

Another article published on this website must guarantee a original article; if it is not a original article, the copyright dispute brings to the author assumes himself. Microsoft is obliged to review the article published on this site, but you need to consider the risk of use of these articles. This site only provides a user exchange technology information and learning gardens, and does not take any risks for technical articles from community users.

转载请注明原文地址:https://www.9cbs.com/read-87927.html

New Post(0)