Provides a proxy for other objects to control access to this object-proxy mode

Source: Internet
Author: User

First, preface

In some cases, a customer does not want or cannot refer directly to an object, and an indirect reference can be achieved by a third party called a "proxy". A proxy object can act as a mediator between the client and the target object, and can remove content and services that the customer cannot see or add additional services that the customer needs through the proxy object.

By introducing a new object to implement the operation of the real object or using the new object as a surrogate for the real object, this implementation mechanism is the proxy mode, which indirectly accesses an object by introducing the proxy object, which is the mode motive of the proxy mode.

In proxy pattern, one class represents the functionality of another class. This type of design pattern belongs to the structural pattern.

In proxy mode, we create an object with an existing object to provide a functional interface to the outside world.

Ii. Basic Concepts

Proxy pattern: Provides a proxy for an object and controls a reference to the original object by the proxy object. Proxy mode in English is called proxy or surrogate, it is an object-structured pattern.

The proxy mode consists of three roles:

Abstract topic Role (SUBJECT): A business method that declares real-world roles through an interface or abstract class.

Proxy: Implements abstract roles, is a proxy for real roles, implements abstract methods through a real-world business logic approach, and can attach its own operations.

Real-World theme Role (Realsubject): Implements an abstract role that defines the business logic that the real role implements to be invoked by the proxy role.

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

The main solution: the problem of direct access to the object, such as: To access the object on the remote machine. In object-oriented systems, some objects, for some reason (such as the overhead of object creation, or the need for security control of some operations, or the need for out-of-process access), give the user or system structure a lot of trouble, and we can add a layer of access to this object when we access it.

when to use: you want to do some control when you access a class.

How to fix: increase the middle tier.

Key code: The implementation is combined with the proxy class.

Application Examples:

1, Windows inside the shortcut.

2, the Pig eight quit to find Miss Gao Cuilan The result is the Monkey King change, can understand: the appearance of Miss Gao Cuilan abstract out, Miss Gao Cuilan himself and the Sun Wukong have realized this interface, pig visit Miss Gao Cuilan time to see this is the sun Wukong, so said Monkey King is Miss Gao Cuilan proxy class.

3, Buy train tickets not necessarily at the railway station to buy, you can also go to the consignment point.

4. 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.

5, Spring AOP.

Advantages: 1, clear responsibilities. 2, high scalability. 3, intelligent.

Disadvantages:

1. Due to the addition of proxy objects between the client and the real topic, some types of proxy patterns can cause requests to be processed more slowly.

2, the implementation of Proxy mode requires additional work, some of the implementation of proxy mode is very complex.

Usage scenarios:

By duty, the following usage scenarios are commonly used:

1, remote agent.

2, virtual agent.

3, Copy-on-write agent.

4. Protect (Protect or Access) proxy.

5, Cache agent.

6, Firewall (Firewall) agent.

7, Synchronization (synchronization) agent.

8, smart Reference agent.

Precautions :

1, complementing adapter mode difference: The adaptor mode mainly changes the interface of the object being considered, while the proxy mode cannot change the interface of the proxy class.

2, and Adorner mode difference: Adorner mode in order to enhance the function, and the proxy mode is to be controlled.

Third, the Code

Basic code implementation:

       Static voidMain (string[] args) {            //instantiating an agentIsubject proxy =NewProxy (); //invoking specific functions through proxiesProxy.            Method (); Proxy.            Function ();        Console.readkey (); }        //Abstract Theme Roles         Public InterfaceIsubject {voidMethod (); voidFunction (); }        //Real Theme Characters         Public classRealsubject:isubject { Public voidMethod () {Console.WriteLine ("This is the Method."); }             Public voidFunction () {Console.WriteLine ("The is Function."); }        }        //Agent Topic role         Public classProxy:isubject {Private ReadOnlyRealsubject _realsubject;  PublicProxy () {_realsubject=NewRealsubject (); }            /// <summary>            ///implement the same interface as the subject, the actual call is the Realsubject method ()/// </summary>             Public voidMethod () {_realsubject.method (); }            /// <summary>            ///implement the same interface as subject, which is actually called the function of Realsubject ()/// </summary>             Public voidFunction () {_realsubject.function (); }        }

Operation Result:

Iv. Summary

Several commonly used proxy modes:

Image proxy: A very common application example of proxy mode is the control of large map browsing.

When a user accesses a Web page through a browser, it does not load a real large image, but instead uses a proxy object to process it, and in the proxy object's method, a thread is used to load a small picture into the client browser, and then a second thread is used in the background to load the large picture into the client. When you need to browse a large picture, the big picture is displayed in the new page. If the user is not finished loading work while browsing the larger map, you can start a thread to display the corresponding prompt. Through the agent technology combined with multi-threaded programming to put the real picture loading into the background to operate, does not affect the foreground picture browsing.

Remote agents: Remote agents can hide the details of the network so that the client does not have to consider the presence of the network. The client can fully assume that the remote business object being proxied is local rather than remote, and that the remote proxy object undertakes most of the network communication work.

Virtual Agent: When an object is loaded with resources, the advantages of virtual agent are very obvious. Virtual proxy mode is a memory-saving technique that takes a lot of memory or handles complex objects that are deferred until they are used.

Provides a proxy for other objects to control access to this object-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.