Java design mode-proxy mode-static proxy

Source: Internet
Author: User

Proxy mode:
Using an environment that cannot directly use object A (a delegate object), you can use object B (the proxy object) to call object A that has reached the effect of calling object a directly, and becomes the proxy mode.

Where a is the delegate object and B is the proxy object.

Advantages and disadvantages of static proxies:

Advantages :

1, the code, you can clearly understand the entrusted Thunder and proxy class.

2, in the compilation period to join, in advance to appoint a good delegate class, proxy class, high efficiency.

Disadvantages :

1, static agent is troublesome, need a lot of proxy class, not easy code maintenance, etc.

When we have more than one target object need to proxy, I need to set up a number of proxy classes, change the original code, changed more is likely to be a problem, it must be re-tested.

2, duplicate code will appear in every corner, the code is too redundant .

3, in the compilation period, the system flexibility is poor, not easy to modify the flexibility

The pseudo code is as follows:

/** Delegate class */

Class a{

public void Method1 () {

System.out.println ("from A method");

};

}

/** proxy class */

Class b{

A;

Public B (a a) {

THIS.A = A;

}

void Method1 () {

if (null! = a) {

A.method1 ();

}

};

}

/**********/

Call procedure

Main () {

b b = new B (new A ());

B.method1 ();

}

/**************************************************** If have mistake, forget elegance, thanks! *************************************************************/

Java design mode-proxy mode-static proxy

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.