Java design mode--proxy mode

Source: Internet
Author: User

I learned the Java Web, and later turned to the android direction, found that people who write Android are geniuses, many of which are also applied to the Java design model, speaking of Java design patterns, we can say a few, such as Android application (singleton mode) , numerous listeners (viewer mode), numerous adapter adapter (adapter modes), Factory mode for processing images ... And so on, but do we have to implement and even use these good design patterns to design their own code? Many of the answers are negative. But think carefully, only follow the slogan "Only write out to be their own", so today will step by step to write ... Learn me happy!
Today I implemented the proxy mode:
First I wrote an interface class:

publicinterface Sourceable {      publicvoidmethod();  }  

Write a class that implements this interface:
public class Source implements sourceable {

@Override  public void method() {      System.out.println("the original method!");  }  

}
Then I wrote a proxy class:
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!");  }  

}
Then we can test it out:
public class Proxytest {

public static void main(String[] args) {      Sourceable source = new Proxy();      source.method();  }  

}
Test results:
Before proxy!
The original method!
After proxy!
We look closely at the code, and the actions performed in the method () methods in the original class are executed in the method () methods in the proxy class. It must have been clear to all of you. Personally feel a bit of interface callback meaning.

Maybe you asked, what is the use of this proxy mode? Is there a proxy mode used in Android? The answer is yes, you remember the process of communication bar, activity and service between the communication, many textbooks say activity and service inter-process communication, the fuck is actually nonsense, if do not do any processing, Service and activity in the same process, if the service is to open a process separately, you need to add android:process:romote when configuring the service. This is the cross-process communication, this is some digression, if you do cross-process communication, we certainly think of is aidl,aidl is actually agent mode, we can think about it according to the above. Inevitably, the following paragraph is an online blog.

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.

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

Java design mode--proxy 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: 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.