Java Design mode Learning-----Agent mode (proxy)

Source: Internet
Author: User

Agent mode (proxy)


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:

Public interface sourceable {public      void method ();  public class Source implements sourceable {        @Override public      Void Method () {          System.out.println ("the Original method! ");      }  }  public class Proxy implements sourceable {        private source source;      Public Proxy () {          super ();          This.source = new Source ();      }      @Override public      Void Method () {          before ();          Source.method ();          Atfer ();      }      private void Atfer () {          System.out.println ("after proxy!");      private void Before () {          System.out.println ("before proxy!");      }  }  


Test:

public class Proxytest {public        static void Main (string[] args) {          sourceable Source = new Proxy ();          Source.method ();      }  }



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!



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Design mode Learning-----Agent 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.