NET design pattern Part Two structural mode (13): Proxy pattern

Source: Internet
Author: User
Tags mul

Agent mode (proxy pattern)

——. NET Design Pattern Series 14

TERRYLEE,2006 year May

Absrtact: In the software system, some objects sometimes because of cross-network or other obstacles, but can not or do not want to directly access another object, if the direct access to the system brings unnecessary complexity, this time can add a layer between the client program and the target object, so that the proxy object to take care of everything in place of the target object. This is the proxy mode to be said in this paper.

Main content

1. Example saying proxy mode

2. The effect of proxy mode and the main points of implementation

......

Overview

In the software system, some objects sometimes because of cross-network or other obstacles, but can not or do not want to directly access another object, if the direct access to the system brings unnecessary complexity, this time can add a layer between the client program and the target object, so that the proxy object in place of the target object. This is the proxy mode to be said in this paper.

Intention

Provides a proxy for other objects to control access to this object.

Structure diagram

Figure 1 Proxy mode structure diagram

Examples of life

The proxy mode provides a mediation to control access to this object. A cheque or bank deposit is an agent of the funds in the account. Cheques are used to replace cash in market transactions and to provide control over the funds on the issuer's account.

Figure 2 Proxy Mode object graph using the bank Deposit certificate Example

Proxy Mode Commentary

In the software system, we are not in the time to overcome obstacles, when we access the resources of a computer on the network, we are crossing the network barrier, when we go to access the database on the server, we are also across the database access barriers, while there are network barriers. Crossing these barriers is sometimes very complex, and if we focus more on dealing with these obstacles, we may lose sight of the business logic that we should be concerned about, and the proxy model will help us solve these problems. As an example of a simple mathematical calculation procedure, this program is only responsible for simple subtraction operations:

//<summary>///Author:terrylee///From :http://terrylee.cnblogs.com/// </summary> Public classmath{ Public DoubleADD (DoubleXDoubley) {returnX +y; }     Public DoubleSub (DoubleXDoubley) {returnX-y; }     Public DoubleMul (DoubleXDoubley) {returnX *y; }     Public DoubleDev (DoubleXDoubley) {returnX/y; }}

If this calculation program is deployed on our local computer, it is very simple to use, and we will not have to consider the proxy mode. But now the problem is that the math class is not deployed to us locally, but instead is deployed on a single server, which means that the math class is not in the same address space as our client program, and we're now dealing with a network barrier that spans the Internet:

Figure 3

This time the method of calling the math class is not as simple as the following, because we still have to consider the network problem, docking the results of the received solution and a series of operations.

/// <summary>///Author:terrylee///From :http://terrylee.cnblogs.com/// </summary> Public classapp{ Public Static voidMain () {Math Math=NewMath (); //to unpack the resulting data received by the docking        DoubleAddresult = Math. ADD (2,3); DoubleSubresult = Math. Sub (2,3); DoubleMulresult = Math. Mul (2,3); DoubleDevresult = Math. Dev (2,3); }}

In order to solve the complexity caused by the network and other obstacles, it leads to the proxy mode, we use a local agent to do everything for the math class, that is, we introduced a layer of indirect layer of the system, as follows

Figure 4

We access the Math data class in Mathproxy and let Mathproxy replace the math class on the network, so we see mathproxy as if it were the local math class, which is in the same address space as the client program:

/// <summary>///Author:terrylee///From :http://terrylee.cnblogs.com/// </summary> Public classmathproxy{PrivateMath Math =NewMath (); //in the following methods, it may be more than just a simple way to call the math class     Public DoubleADD (DoubleXDoubley) {returnMath.    ADD (x, y); }     Public DoubleSub (DoubleXDoubley) {returnMath.    Sub (x, y); }     Public DoubleMul (DoubleXDoubley) {returnMath.    Mul (x, y); }     Public DoubleDev (DoubleXDoubley) {returnMath.    Dev (x, y); }}

It is now possible to say that we have implemented the proxy for the math class, and one problem is that we called the original implementation class math in the Mathproxy class, but math does not necessarily implement all the methods, in order to force the math class to implement all the methods, on the other hand, For us to manipulate objects more transparently, we add a layer of abstraction on the basis of the math class and the Mathproxy class, that is, they all implement the IMath interface, as follows:

The schematic code is as follows:

//<summary>///Author:terrylee///From :http://terrylee.cnblogs.com/// </summary> Public Interfaceimath{DoubleADD (DoubleXDoubley); DoubleSub (DoubleXDoubley); DoubleMul (DoubleXDoubley); DoubleDev (DoubleXDoubley);} The math class and the Mathproxy class implement the IMath interface, respectively: Public classmathproxy:imath{//} Public classmath:imath{//at this point we can use the Mathproxy class in the client program just like the Math class:/// <summary>///Author:terrylee///From :http://terrylee.cnblogs.com/// </summary> Public classapp{ Public Static voidMain () {Mathproxy proxy=NewMathproxy (); DoubleAddresult = proxy. ADD (2,3); DoubleSubresult = proxy. Sub (2,3); DoubleMulresult = proxy. Mul (2,3); DoubleDevresult = proxy. Dev (2,3); }}

Here the whole process of using proxy mode is complete, and looking back at our solution is nothing more than adding an indirect layer between the client program and the math class, which is one of the more common ways to solve problems. In addition, for the interface IMath in the program, it is not necessary, in most cases, in order to maintain the transparency of the object operation, and enforce the implementation of the class to implement the proxy class to invoke all the methods, we will let them implement the same interface. But we say that the proxy class is just to some extent representing the original implementation class, so they can sometimes not be implemented on the same interface.

Effect and key points of realization

The proxy mode is different depending on the type, and the effect varies:

1. Remote proxy: Provides a local representative object for an object that is 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 Ambassador (Ambassador). 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. The client can fully assume that the object being proxied is local rather than remote, and that the proxy object is responsible for most of the network communication work. Because the customer may not be aware of starting a time-consuming remote call, the customer has no need to be prepared.

2. Virtual Proxy: Create a resource-intensive object as needed so that the object is actually created only when it is needed. The advantage of using the virtual proxy pattern is that the proxy object can load the Proxied object when necessary, and the agent can optimize the loading process as necessary. When a module is loaded with resources, the benefits of virtual proxies are obvious.

3. Copy-on-write Proxy: One of the virtual agents. Delay replication (cloning) until the client needs it, to take real action.

4. Protection (Protect or access) Proxy: Controls access to an object and, if necessary, provides different levels of usage rights to different users. The benefit of the protection agent is that it can check the user's permissions at run time and then decide to pass the call to the Proxied object after verification.

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

6. Firewall (Firewall) Proxy: Protects the target from being approached by a malicious user.

7. Synchronous (synchronization) Proxy: Enables several users to use one object at a time without conflict.

8. Smart Reference Proxy: provides some extra action when an object is referenced, such as the number of times a call to this object is recorded.

Summarize

In the software system, the addition of an intermediate layer is our common approach to solve the problem, this proxy mode gives us a good implementation.

Resources

Erich Gamma, etc., "design mode: The basis of reusable object-oriented software", Mechanical industry Press

Robert C.martin, "Agile Software Development: principles, patterns and practices", Tsinghua University Press

Shanhong, Java and patterns, electronic industry publishing house

Alan Shalloway James R. Trott, "Design Patterns explained", China Power Press

MSDN Webcast "C # Object-oriented design mode discussion on (): Proxy mode (structured mode)"

NET design pattern Part Two structural mode (13): Proxy pattern

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.