A classic example that gives you a thorough understanding of the Java callback mechanism

Source: Internet
Author: User

Original address: http://blog.csdn.net/xiaanming/article/details/17483273

Previously did not understand what called the callback, every day listen to people say add a callback method What, my heart think I grass, what is called callback method ah? And then I find it on the Internet Ah find, find a lot is not very clear, now know, called callback: is a class called in Class B of a method C, and then in the Class B in turn called the method in Class A d,d this method called callback method, so that you are not a bit dizzy, in fact, I just started to understand , looked at the other people say the more classic callback method:

    • Class a implements interface callback callback-- background 1
    • Class A contains a reference to Class B b-- background 2
    • Class B has a method with a parameter of CallBack F (CallBack CallBack)-- background 3
    • Object A of a calls B method F (CallBack CallBack)--a class calls a method of Class B
    • Then B can call a method in the F (CallBack CallBack) method--b class calls a method of Class D

We all like to use the phone example, well, in order to keep up with the times, I also use this example, I use this example of asynchronous plus callback

One day Xiao Wang encountered a difficult problem, the question is "1 + 1 =?", on the phone to ask Xiao Li, Xiao Li suddenly did not know, and Xiao Wang said, and so I finish the things on hand, to think about the answer, Xiao Wang will not be silly to take the phone to wait for Xiao Li's answer, so Xiao Wang said to Xiao Li, I have to go shopping, You know the answer to call me to tell me, so hang up the phone, do their own things, after one hours, Xiao Li hit the small Wang's phone, told him the answer is 2

01./** 02. * This is a callback interface 03. * @author xiaanming 04. * 05. */  06.public interface CallBack {  .    /**.     * This is the function that Xiao Li will call when he knows the answer. Tell Xiao Wang, the callback function.     * @param result is answer ten.     */One  .    public void Solve (String result);  

01./** 02. * This is Xiao Wang 03. * @author xiaanming 04. * Implemented a callback interface callback, equivalent to-----> background 105.    */06.public class Wang implements CallBack {07.     /** 08.     * Reference to the small Li object 09.     * Equivalent to-----> background 210.    */11.   Private Li Li;    12.13.     /** 14.     * The construction method of Xiao Wang, holding the reference of Xiao Li 15.     * @param Li 16.    */17.        Public Wang (Li Li) {18.  This.li = Li;      19.} 20.     21./** 22.     * Xiao Wang in this way to ask Xiao Li's question 23. * @param question is the question that Xiao Wang wants to ask, 1 + 1 =?    24. */25.        public void Askquestion (final String question) {26.        Here a thread is asynchronous, 27.            New Thread (New Runnable () {28.            @Override 29.                public void Run () {30.                 /** 31.                 * Xiao Wang calls the method of small Lee, here Register callback interface 32.                 * This is equivalent to method C 33 of Class A invocation B.                */34.   Li.executemessage (wang.this, question);        35.} 36.  }). Start ();        37.38.        The small net asks the question to hang off the telephone to do other things, exaggeration Street went to 39.  Play ();  40.} 41. public void Play () {43. SYSTEM.OUT.PRINTLN ("I'm going to go shopping");  44.} 45.     46./** 47.     * Xiao Li know the answer after calling this method to tell Xiao Wang, is the so-called Xiao Wang's callback method 48.    */49.    @Override 50.        public void Solve (String result) {51.  System.out.println ("Xiao Li told Xiao Wang the answer is--->" + result);      52.} 53.   54.}

01./** 02. * This is Xiao Li 03. * @author xiaanming 04. * 05. */  06.public class Li {  .    /**.     * Equivalent to Class B with parameters of callback callback F ()----> Background 309.     * @param callBack   .     * @param question  The questions asked by Xiao Wang.     * *  .    public void ExecuteMessage (CallBack CallBack, String question) {  .        System.out.println ("Xiao Wang asked questions--->" + question);  .        It takes a long time to simulate Xiao Li's own affairs  .        for (int i=0; i<10000;i++) {  .              .        }  .          /**.         * When Xiao Li finishes his own business, he thinks the answer is 2.         * *  .        String result = "answer is 2";  .        /**.         So I called Xiao Wang and called Xiao Wang's method.         * This is equivalent to Class B, which in turn calls a method D.         * *  .        Callback.solve (result);   .    .      35.}  

01./** 02. * Test Class 03. * @author xiaanming 04. * 05. */  06.public class Test {  .    public static void Main (String[]args) {  .        /**.         * New one Xiao Li.         */One  .        Li li = new Li ();  .        /**.         * New A little Wang.         * *  .        Wang Wang = new Wang (LI);  .        /**.         * Xiao Wang asked Xiao Li's question.         * *  .        Wang.askquestion ("1 + 1 =?");  23.}  

A classic example that gives you a thorough understanding of the Java callback mechanism

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.