Proxy mode (structural mode)

Source: Internet
Author: User

1. Questions

In an object-oriented system, some objects for some reason (such as the overhead of creating objects, or the need for security control of some operations, or the need for out-of-process access, etc.), direct access to the caller will cause trouble, so how to do without loss of interface transparency, to solve the problem?

2. Solutions

By erecting an intermediate layer, let this layer of the middle tier to solve the above problems, that is, what we call the agent. The approximate structure conversion diagram is as follows:

Converted Into

A complex system A, by assuming an intermediate layer C, processes the complexity in a, and provides a more flexible, C-to-B solution to meet B requirements.

Case one,. NET in WebService technology provides a good reference. The code is as follows:

        #regionDistributed services based on. Net WebService Public InterfaceIuser {voidGetUser (); voidAddUser (); voidUpdateUser (); voidDeleteUser (); }         Public classUser:iuser { Public voidAddUser () {} Public voidDeleteUser () {} Public voidGetUser () {} Public voidUpdateUser () {}}#endregion        #regionClient Invoke Program/// <summary>        ///The role of the proxy class is to mask the details of the distributed communication (such as the processing of the Protocol), and the client calling code does not need to pay attention to these details///just focus on the business/// </summary>         Public classUserproxy:iuser { PublicUserproxy () {//a SOAP wrapper created on an object            }             Public voidGetUser () {//a SOAP wrapper for object access//send SOAP data to execute business code in distributed services//If there is a return value, accept the return value SOAP data, unpack, convert to C # code            }             Public voidAddUser () {} Public voidDeleteUser () {} Public voidUpdateUser () {}}

With the Userproxy class, this class completes the related processing of the SOAP protocol, and then the client calls the class without needing to care about the processing of the SOAP protocol, only to be concerned with the business code invocation, which is the role of the proxy class.

Case two, StringBuilder's Copy-on-wirte technology

                New StringBuilder ("HelloWorld");                 New StringBuilder ("HelloWorld");                 New StringBuilder ("HelloWorld");

The above code creates 3 StringBuilder classes, because of string instance uniqueness and string invariance, about string instance uniqueness and string invariance, reference character, string, and literal processing of string types, you can draw the following structure diagram:

3 StringBuilder instances point to the "Hello World" string instance, but when one of the StringBuilder executes the Replace method, if you follow this structure, when an instance modifies the string, The other two instances will also share this modification. But in fact, StringBuilder is not doing so. Because StringBuilder represents a mutable string. So the string that corresponds to the StringBuilder instance changes, Next look at how StringBuilder uses the copy on write technique to modify a string instance, with the following code:

StringBuilder Sbone =NewStringBuilder ("Hello World"); StringBuilder Sbtwo=NewStringBuilder ("Hello World"); StringBuilder Sbthree=NewStringBuilder ("Hello World"); Sbthree.replace ("Hello World","Small Super"); Console.WriteLine (sbthree);//output Small Super

Depending on the output, it is found that the same StringBuilder instance can be modified, but the string object cannot be modified. The approximate structure diagram is as follows:

This is the copy on write technology, and the StringBuilder class is the equivalent of a proxy class, which proxies a string class, so that the previously unable to modify the instance of the strings can be modified, it is a mutable string proxy class.

Proxy mode (structural 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: 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.