The eight-agent mode of Java design mode (proxy)

Source: Internet
Author: User

In fact, each pattern name indicates the role of the mode, proxy mode is more than one proxy class out, for the original object to do some operations, such as we rent the house when back to find intermediary, why? Because you do not have comprehensive information about the housing in the area, I hope to find a more familiar person to help you do, the agent here is the meaning. If we have a lawsuit, we need a lawyer, because the lawyer has expertise in law and can act on our behalf and express our ideas. Take a look at the diagram first:

According to the above explanation, the proxy mode is easier to understand, we look at the code:

[Java]View Plaincopy
    1. Public interface Sourceable {
    2. public void Method ();
    3. }
[Java]View Plaincopy
    1. public class Source implements sourceable {
    2. @Override
    3. public void Method () {
    4. System.out.println ("The original method!");
    5. }
    6. }
[Java]View Plaincopy
  1. public class Proxy implements sourceable {
  2. private source source;
  3. Public Proxy () {
  4. Super ();
  5. This.source = new Source ();
  6. }
  7. @Override
  8. public void Method () {
  9. Before ();
  10. Source.method ();
  11. Atfer ();
  12. }
  13. private void Atfer () {
  14. System.out.println ("after proxy!");
  15. }
  16. private void before () {
  17. System.out.println ("before proxy!");
  18. }
  19. }

Test class:

[Java]View Plaincopy
    1. public class Proxytest {
    2. public static void Main (string[] args) {
    3. sourceable Source = new Proxy ();
    4. Source.method ();
    5. }
    6. }

Output:

Before proxy!
The original method!
After proxy!

Application Scenarios for Proxy mode:

If existing methods need to be improved when used, there are two ways to do this:

1, modify the original method to adapt. This violates the principle of "open for expansion, closed for modification".

2, is to use a proxy class to call the original method, and the resulting results are controlled. This approach is proxy mode.

Using the proxy mode, you can divide the function more clearly and help to maintain later!

The eight-agent mode of Java design mode (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.