How to think about Java design methods implement the next method automatically after a method is executed

Source: Internet
Author: User

When programming today, it suddenly comes to mind that in some asynchronous operations or Android native libraries, we need to implement some methods,

These methods only need to be specifically implemented by us, and then they will be called automatically at the appropriate time!

For example Asynctask, the Postresult () method that executes the result after playing Doinbackground () is executed, but we have not set their order of execution.

Previously only knew their operation principle, but did not know the concrete realization method, thought up, this is a quite good code design, so decided to study a bit.

Because the first way is to implement the methods that need to be implemented, so that the way to implement is the interface,

Think for a long time, still did not think of the interface inside how to set the order of the method, search found no related results, had to look at the source of Asynctask, hope to get answers from the source code!

And the source is quite concise and powerful, directly gives the answer I want: the implementation method is called order control, not through the interface to achieve, but through the abstract class!

And the implementation of the order of the specific control procedures, is in a fixed method of controlling: For example, Asynctask execute (the implementation of the process can be easily viewed in the IDE, not to the specific analysis)

Here is a sample code that I imitated to print all the contents of a string array:

public class interface{public static void Main (string[] args) {string[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; New Order (b) {@Override protected string Onfirst (string s) {System.out.prin                TLN (s);            return null;                } @Override protected String OnNext (string s) {System.out.println (s);            return null;            } @Override protected void oncompleted (String s) {System.out.println (s);    }}.start (); }}
The abstract class order{private string[] A, which implements the sequential invocation of the method. private int i = 0; Public Order (string[] s) {THIS.A = s; }
This abstract object is started by this method, and the specific procedure of the control method invocation is starting from here public string[] Start () {

Onfirst (a[i++]); Executor (); return A; } public void Executor () {
The last one needs to be executed at completion, or the next method can be executed to execute the completed method while (I < a.length-2 && a[i]! = null) {a[i++] = OnNext (a[i ]); }//Returns the last element of the string array oncompleted (A[a.length-1]); }//The following three methods are used to implement the new object, and I just want to implement the method in order to call, simply give the corresponding order of the string
If you want to achieve more complex functions, the same principle, specific problems specific analysis
protected abstract String Onfirst (string s); protected abstract String OnNext (string s); protected abstract void oncompleted (String s);}

  

How to think about Java design methods implement the next method automatically after a method is executed

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.