15. Structural Design Pattern-proxy pattern)

Source: Internet
Author: User
    • Definition

Provide a proxy or a place for other objects to control access to this object. When the customer initiates a request to the proxy object for the first time, the proxy instantiates the real object and sends the request to it. In the future, all client requests are sent to the encapsulated real object through the proxy.

There are four proxy modes: Remote and virtual proxy modes:

1. Virtual Proxy: the proxy will not generate a real consumption proxy until it is necessary (first request ). It is used to store real objects that take a long time for an instance.

2. remote proxy: The local proxy object controls a remote object.

3. Protection proxy (Security Proxy): the proxy checks the permissions required to call real objects.

4. Smart reference: when calling a real object, the proxy handles other things.

(1) calculate the number of references of a real object and release it automatically when no reference is provided.

(2) objects that are referenced for the first time (stored on hard disk, memory, database, etc.

(3) check whether the real object is locked during access to ensure that no other object will change it.

The UML class diagram is as follows:

The relationships between classes and objects are as follows:

1. proxy: maintain a reference so that the proxy can access the object. If the realsubject and subject interfaces are the same, the proxy will reference subject; provide the same interface as the subject interface, the proxy can be used to replace objects. A bit like the adapter mode; controls access to the object and is responsible for creating and deleting it; other functions depend on the proxy type.

* Remote proxy: It is responsible for encoding requests and their parameters, and sending encoded requests to entities in different address spaces.

* Virtual Proxy: Other information of an object can be cached to delay its access.

* Protection proxy: Check whether the caller's request has necessary permissions.

2. Subject (abstract object interface): defines the same interface for the realsubject object and proxy, so that realsubject can be accessed anywhere Using proxy.

3. realsubject (entity): defines the entity of the proxy.

The following figure shows the order of typical applications:

    • Example 1-mathematical proxy

The following example shows the remote proxy of an application to access another application.ProgramObjects in the domain.

The UML class diagram is as follows:

Code

    //  Imath 
Public Interface Imath
{
Double Add ( Double X, Double Y );
Double Sub ( Double X, Double Y );
Double Mul ( Double X, Double Y );
Double Div ( Double X, Double Y );
}
// Entity to implement two interfaces. The marshalbyrefobject interface allows communication between different program domains (processes)
Class Math: marshalbyrefobject, imath
{
Public Double Add ( Double X, Double Y)
{
Return X + Y;
}
Public Double Sub ( Double X, Double Y)
{
Return X - Y;
}
Public Double Mul ( Double X, Double Y)
{
Return X * Y;
}
Public Double Div ( Double X, Double Y)
{
Return X / Y;
}
}
// Remote proxy object proxy, including an instance of math, which is used to process, add, subtract, multiply, and divide
Class Mathproxy: imath
{
Math math;
Public Mathproxy ()
{
Math = New Math ();
}
Public Double Add ( Double X, Double Y)
{
Return Math. Add (x, y );
}
Public Double Sub ( Double X, Double Y)
{
Return Math. sub (x, y );
}
Public Double Mul ( Double X, Double Y)
{
Return Math. Mul (x, y );
}
Public Double Div ( Double X, Double Y)
{
Return Math. Div (x, y );
}
}

// Customer application testing
Class Client
{
[Stathread]
Static Void Main ( String [] ARGs)
{
Mathproxy P = New Mathproxy ();
Console. writeline ( " 4 + 2 = {0} " , P. Add ( 4 , 2 ));
Console. writeline ( " 4-2 = {0} " , P. sub ( 4 , 2 ));
Console. writeline ( " 4*2 = {0} " , P. Mul ( 4 , 2 ));
Console. writeline ( " 4/2 = {0} " , P. Div ( 4 , 2 ));
Console. Read ();
}
}

 

 

    • Strengths and weaknesses

When an object is generated on a remote machine through the network, the speed may be slow. In this case, the remote proxy mode can be applied to mask the process of generating the object from the network, and the system speed will be faster; for the loading of large images, the virtual proxy mode allows loading in the background, and the proxy object used in the foreground optimizes the overall running speed; the protect proxy can verify the permission to reference real objects.

The disadvantage of the proxy mode is that the request processing speed slows down and extra work is required to implement the proxy mode.

    • Application scenarios

The classification of the proxy mode described above is the scenario where it can be applied.

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.