JAVA CallBack mechanism (callback) details, CallBack callback

Source: Internet
Author: User

JAVA CallBack mechanism (callback) details, CallBack callback

Preface

I recently learned about java and came into contact with the CallBack mechanism (CallBack ). At first glance, I felt quite confused, and I found some explanations on the Internet. Either I mentioned them in a word or I simply defined CallBack. Of course, after I understand the callback, I can go back to the various explanations on the Internet, which is indeed no problem. However, for beginners, a gradual process is missing. Here, I will describe my personal understanding of the callback mechanism in the order from shortest to deep. If anything is inappropriate, I hope you will not give me any further advice!

Before getting started, I first thought like a scenario: kindergarten children have just learned the addition of less than 10.

Chapter 2. The origin of the story

The kindergarten teacher wrote a formula "1 + 1 =" on the blackboard. James asked me to fill in the blanks.

Since I have learned the addition of less than 10, James can calculate this question on his own. The code used to simulate this process is as follows:

1 public class Student 2 {3 private String name = null; 4 5 public Student (String name) 6 {7 // TODO Auto-generated constructor stub 8 this. name = name; 9} 10 11 public void setName (String name) 12 {13 this. name = name; 14} 15 16 private int calcADD (int a, int B) 17 {18 return a + B; 19} 20 21 public void fillBlank (int a, int B) 22 {23 int result = calcADD (a, B); 24 System. out. println (name + "mental arithmetic:" + a + "+" + B + "=" + result); 25} 26}

During the fillBalnk process, James calculated clacADD and wrote the result in a space. The test code is as follows:

1 public class Test 2 {3 public static void main (String [] args) 4 {5 int a = 1; 6 int B = 1; 7 Student s = new Student ("James"); 8 s. fillBlank (a, B); 9} 10}

 

The running result is as follows:

James mental arithmetic: 1 + 1 = 2

This process is completely completed by the Student class instance object and does not involve the callback mechanism.

Chapter 2. Finding exceptions for kindergarten teachers

During the recess, the kindergarten teacher wrote "168 + 291 =" on the blackboard to let James finish the work and then went back to the office.

Wipe the flowers! Why can't all teachers go with James? Clearly beyond the outline of the good! At this time, James was obviously unable to rely on mental computing as above. When he was being forced, Xiao Hong from the class handed in a calculator (profiteer) that could only calculate addition )!!!! James knew how to use the calculator, so he obtained the result through the calculator and completed filling in the blanks.

The calculator code is:

1 public class Calculator2 {3     public int add(int a, int b)4     {5         return a + b;6     }7 }

Modify the Student class and add a method to use the calculator:

1 public class Student 2 {3 private String name = null; 4 5 public Student (String name) 6 {7 // TODO Auto-generated constructor stub 8 this. name = name; 9} 10 11 public void setName (String name) 12 {13 this. name = name; 14} 15 16 @ SuppressWarnings ("unused") 17 private int calcADD (int a, int B) 18 {19 return a + B; 20} 21 22 private int useCalculator (int a, int B) 23 {24 return new Calculator (). add (a, B); 25} 26 27 public void fillBlank (int a, int B) 28 {29 int result = useCalculator (a, B); 30 System. out. println (name + "use calculator:" + a + "+" + B + "=" + result); 31} 32}

The test code is as follows:

1 public class Test 2 {3 public static void main (String [] args) 4 {5 int a = 168; 6 int B = 291; 7 Student s = new Student ("James"); 8 s. fillBlank (a, B); 9} 10}

The running result is as follows:

James uses a calculator: 168 + 291 = 459

The callback mechanism is not involved in this process, but some of James's work has been transferred and is assisted by a calculator.

3. the kindergarten teacher is back

I found that James completed the three-digit addition. The teacher thought James was very smart and he was a plastic talent. So I wrote "26549 + 16487 =" on the blackboard, asking James to fill in the blanks before class, and then go back to the office.

James looked at his friends outside the classroom. Don't go out again. This lesson is about to be abandoned !!!! Looking at the calculator that Xiaohong handed in again, Xiao Mingxin gave birth to a plan: Let Xiaohong do the work.

James told Xiaohong the question "26549 + 16487 =", pointed out the specific location of the result, and then went out to play happily.

Here, we do not implement Xiaohong separately, but regard this calculator and Xiaohong, which can only be used as an addition, as a whole, and a super calculator that will fill in the results. The super calculator needs to pass two parameters and fill in the blanks. These parameters need to be notified in advance, that is, James wants to expose some of his methods to Xiaohong, the simplest way is to tell Xiaohong about your reference and two Addons.

Therefore, the add method of the Super Calculator should contain two operands and James's reference. The Code is as follows:

1 public class SuperCalculator2 {3     public void add(int a, int b, Student  xiaoming)4     {5         int result = a + b;6         xiaoming.fillBlank(a, b, result);7     }8 }

James does not need mental computing or a calculator. Therefore, he only needs a method to seek help from Xiaohong. The Code is as follows:

1 public class Student 2 {3 private String name = null; 4 5 public Student (String name) 6 {7 // TODO Auto-generated constructor stub 8 this. name = name; 9} 10 11 public void setName (String name) 12 {13 this. name = name; 14} 15 16 public void callHelp (int a, int B) 17 {18 new SuperCalculator (). add (a, B, this); 19} 20 21 public void fillBlank (int a, int B, int result) 22 {23 System. out. println (name + "Help Xiaohong computing:" + a + "+" + B + "=" + result); 24} 25}

The test code is as follows:

1 public class Test 2 {3 public static void main (String [] args) 4 {5 int a = 26549; 6 int B = 16487; 7 Student s = new Student ("James"); 8 s. callHelp (a, B); 9} 10}

The running result is:

James turned to Xiaohong for computation: 26549 + 16487 = 43036

The execution process is as follows: James calls the add (new SuperCalculator () method through his callHelp method, and uses his reference (this) as a parameter during the call, after using the calculator to get the result, XiaoHong calls back James's fillBlank method and places the result in a space on the blackboard.

Lamp! At this point, the callback function was officially launched. James's fillBlank method is the callback function we often call.

In this way, we can clearly see that for the completion of the teacher's fill in the blank question task, James does not need to wait until the addition is completed and the results are filled in on the blackboard before he can play with friends, filling in the blanks is done by the super calculator Xiaohong. The advantages of callback have already begun to be reflected.

Chapter 4. Mother-in-law at the door

There is an old mother-in-law with white hair at the door of the kindergarten, where the rain and rain are everywhere to sell some junk food that is about to expire. I am confused because I am too old to know how much I have earned. One day, she accidentally heard James and her friends brag about how she could be brave with the help of Xiaohong. As a result, her mother-in-law decided to find the red card super calculator to be her little helper, and provided a pack of Wei long spicy strips as compensation. Xiaohong failed to resist the temptation and agreed.

Looking back at the code in the previous chapter, we found that the add method of the red card super calculator requires two integer variables and a Student object, but she is not a Student, but a small vendor, you must make changes here. In this case, we naturally think of inheritance and polymorphism. If Xiao Ming and his mother-in-law are allowed to inherit from a parent class, we only need to input a parent class reference to the red card super calculator.

However, in actual use, considering java's single inheritance and not wanting to expose too many things to others, we use the method inherited from the interface to work with internal classes.

In other words, XiaoHong hopes to continue providing computing services to the children in the class, as well as providing accounting services to her mother-in-law, or even expanding other people's business in the future, so she agreed to all the customers for a unified processing, that is, the operations they need and what should they do after the computation. In this unified method, XiaoHong made an interface and provided it to everyone. The Code is as follows:

1 public interface doJob2 {3     public void fillBlank(int a, int b, int result);4 }

Because it was inspired by helping James fill in the blanks, XiaoHong kept his initial mind and considered all businesses as fillBlank.

At the same time, XiaoHong modified his calculator so that it could simultaneously process different persons implementing the doJob interface. The Code is as follows:

1 public class SuperCalculator2 {3     public void add(int a, int b, doJob  customer)4     {5         int result = a + b;6         customer.fillBlank(a, b, result);7     }8 }

After James and her mother-in-law get this interface, as long as the interface is implemented, it is equivalent to telling Xiaohong the solution after the result is obtained in a unified mode, and using the internal class as mentioned earlier, the Code is as follows:

James's:

1 public class Student 2 {3 private String name = null; 4 5 public Student (String name) 6 {7 // TODO Auto-generated constructor stub 8 this. name = name; 9} 10 11 public void setName (String name) 12 {13 this. name = name; 14} 15 16 public class doHomeWork implements doJob17 {18 19 @ Override20 public void fillBlank (int a, int B, int result) 21 {22 // TODO Auto-generated method stub23 System. out. println (name + "Help little red computing:" + a + "+" + B + "=" + result); 24} 25 26} 27 28 public void callHelp (int, int B) 29 {30 new SuperCalculator (). add (a, B, new doHomeWork (); 31} 32}

Old Mother-in-law's:

1 public class Seller 2 {3 private String name = null; 4 5 public Seller (String name) 6 {7 // TODO Auto-generated constructor stub 8 this. name = name; 9} 10 11 public void setName (String name) 12 {13 this. name = name; 14} 15 16 public class doHomeWork implements doJob17 {18 19 @ Override20 public void fillBlank (int a, int B, int result) 21 {22 // TODO Auto-generated method stub23 System. out. println (name + "help small red:" + a + "+" + B + "=" + result + "Yuan "); 24} 25 26} 27 28 public void callHelp (int a, int B) 29 {30 new SuperCalculator (). add (a, B, new doHomeWork (); 31} 32}

The test procedure is as follows:

1 public class Test 2 {3 public static void main (String [] args) 4 {5 int a = 56; 6 int B = 31; 7 int c = 26497; 8 int d = 11256; 9 Student s1 = new Student ("James"); 10 Seller s2 = new Seller ("Old Mother-in-law"); 11 12 s1.callHelp (a, B ); 13 s2.callHelp (c, d); 14} 15}

The running result is as follows:

Xiao Ming asked for help Xiaohong computing: 56 + 31 = 87 old mother-in-law for help Xiaohong accounting: 26497 + 11256 = 37753 yuan

Last words

It is obvious that Xiaohong has already taken this matter as a career, and she can see the name doJob she gave to the interface.

Some may ask why the old mother-in-law can earn so much money at a stall? Is there a problem with your focus !! Here we are talking about the callback mechanism !!

I only know that after that, Xiaohong's business continued to expand and finally bought the first house in his life with the money he earned before his graduation from kindergarten.

Complete !!!

 

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.