Home > Java > javaTutorial > Detailed explanation of JAVA's callback mechanism CallBack

Detailed explanation of JAVA's callback mechanism CallBack

高洛峰
Release: 2017-01-24 13:34:28
Original
1143 people have browsed it

Preface

CallBack means callback. People who are familiar with Windows programming will be familiar with the four words "callback function", but Java programmers may not know much about it. "Callback function" or "callback method" is a very important concept in software design and development. Mastering the idea of ​​"callback function" is very necessary for programmers (no matter which language is used).

I recently learned Java and came into contact with the callback mechanism (CallBack). It felt quite confusing when I first met it, and the relevant explanations I searched on the Internet were either given in one sentence, or they were relatively simple and seemed to provide a definition for CallBack. Of course, after I understood the callbacks, I went to read various explanations on the Internet, and there was really no problem. However, for me as a beginner, there is a step-by-step process missing. Here, I will describe my personal understanding of the callback mechanism in order from shallower to deeper. If there is anything wrong, please feel free to enlighten me!

Before you begin, imagine a scene: Kindergarten children have just learned addition within 10.

Chapter 1. The origin of the story

The kindergarten teacher wrote an equation "1 + 1 =" on the blackboard, and Xiao Ming filled in the blanks.

Since he has learned addition within 10, Xiao Ming can calculate this problem completely by himself. The code to simulate the process is as follows:

public class Student
{
private String name = null;
public Student(String name)
{
// TODO Auto-generated constructor stub
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
private int calcADD(int a, int b)
{
return a + b;
}
public void fillBlank(int a, int b)
{
int result = calcADD(a, b);
System.out.println(name + "心算:" + a + " + " + b + " = " + result);
}
}
Copy after login

Xiao Ming When filling in the blanks (fillBalnk), the students did a mental calculation (clacADD) and found that the result was 2, and wrote the result in the blank. The test code is as follows:

public class Test
{
public static void main(String[] args)
{
int a = ;
int b = ;
Student s = new Student("小明");
s.fillBlank(a, b);
}
}
Copy after login
Copy after login

The running results are as follows:

Xiao Ming’s mental arithmetic: 1 + 1 = 2

The process is complete It is completed by the instance object of the Student class alone, and the callback mechanism is not involved.

Chapter 2. The Kindergarten Teacher's Finding Trouble

During the break, the kindergarten teacher had a sudden idea and wrote "168 + 291 =" on the blackboard for Xiao Ming to complete, and then returned to the office. .

花开! Why do all the teachers have trouble with Xiao Ming? It's obviously over the top, okay! At this time, it was obvious that Xiao Ming could no longer rely on mental arithmetic as above. When he was confused, Xiao Hong, a classmate in the class, handed over a calculator that could only calculate addition (profiteer)! ! ! ! Xiao Ming happened to know how to use a calculator, so he used the calculator to calculate the results and fill in the blanks.

The code of the calculator is:

public class Calculator
{
public int add(int a, int b)
{
return a + b;
}
}
Copy after login

Modify the Student class and add the method of using the calculator:

public class Student
{
private String name = null;
public Student(String name)
{
// TODO Auto-generated constructor stub
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
@SuppressWarnings("unused")
private int calcADD(int a, int b)
{
return a + b;
}
private int useCalculator(int a, int b)
{
return new Calculator().add(a, b);
}
public void fillBlank(int a, int b)
{
int result = useCalculator(a, b);
System.out.println(name + "使用计算器:" + a + " + " + b + " = " + result);
}
}
Copy after login

The test code is as follows:

public class Test
{
public static void main(String[] args)
{
int a = ;
int b = ;
Student s = new Student("小明");
s.fillBlank(a, b);
}
}
Copy after login
Copy after login

The running results are as follows:

Xiao Ming uses the calculator: 168 + 291 = 459

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

3. The kindergarten teacher came back

and found that Xiao Ming had completed the addition of three digits. The teacher thought that Xiao Ming was very smart and a malleable talent. So he wrote "26549 + 16487 = " on the blackboard and asked Xiao Ming to fill in the blanks before going to class, and then returned to the office.

Xiao Ming looked at the friends having fun outside the classroom and couldn't help feeling sad. If we don’t go out to play, this recess will be ruined! ! ! ! Looking at the calculator that Xiao Hong handed over again, Xiao Ming came up with a plan: let Xiao Hong do it for her.

Xiao Ming told Xiao Hong that the topic was "26549 + 16487 = ", then pointed out the specific location where the result should be filled in, and then went out to play happily.

Here, instead of implementing Xiaohong separately, we consider this calculator that can only perform addition and Xiaohong as a whole, a super calculator that can calculate results and fill in the blanks. The parameters that need to be passed to this super calculator are two addends and the positions to be filled in, and these contents need to be informed by Xiao Ming in advance, that is, Xiao Ming wants to expose part of his method to Xiao Hong. The simplest way is to Tell Xiaohong the quote and the two addends.

Therefore, the add method of the super calculator should contain two operands and a reference to Xiao Ming himself. The code is as follows:

public class SuperCalculator
{
public void add(int a, int b, Student xiaoming)
{
int result = a + b;
xiaoming.fillBlank(a, b, result);
}
}
Copy after login

Xiao Ming has now There is no need for mental arithmetic or the use of a calculator, so there is only a way to ask Xiaohong for help. The code is as follows:

public class Student
{
private String name = null;
public Student(String name)
{
// TODO Auto-generated constructor stub
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
public void callHelp (int a, int b)
{
new SuperCalculator().add(a, b, this);
}
public void fillBlank(int a, int b, int result)
{
System.out.println(name + "求助小红计算:" + a + " + " + b + " = " + result);
}
}
Copy after login

Test The code is as follows:

public class Test
{
public static void main(String[] args)
{
int a = ;
int b = ;
Student s = new Student("小明");
s.callHelp(a, b);
}
}
Copy after login

The running result is:

Xiao Ming asks Xiao Hong for help to calculate: 26549 + 16487 = 43036
Execution The process is: Xiao Ming calls the add method of Xiao Hong (new SuperCalculator()) through his own callHelp method. When calling, he passes in his own reference

(this) as a parameter. Xiao Hong After using the calculator to get the result, Xiao Ming's fillBlank method is called back and the result is filled in the blank space on the blackboard.

Light, light, light! At this point, the callback function officially appears. Xiao Ming's fillBlank method is what we often call the callback function.

Through this method, it can be clearly seen that for the task of completing the teacher's fill-in-the-blank question, Xiao Ming no longer needs to wait until the addition is completed and the result is filled in on the blackboard before he can follow Xiao Ming. The friends had fun, and the super calculator Xiaohong did the job of filling in the blanks. The advantages of the pullback have already begun to show.

Chapter 4. Mother-in-law at the door

幼稚园的门口有一个头发花白的老婆婆,每天风雨无阻在那里摆着地摊卖一些快过期的垃圾食品。由于年纪大了,脑子有些糊涂,经常算不清楚自己挣了多少钱。有一天,她无意间听到了小明跟小伙伴们吹嘘自己如何在小红的帮助下与幼师斗智斗勇。于是,婆婆决定找到小红牌超级计算器来做自己的小帮手,并提供一包卫龙辣条作为报酬。小红经不住诱惑,答应了。

回看一下上一章的代码,我们发现小红牌超级计算器的add方法需要的参数是两个整型变量和一个Student对象,但是老婆婆她不是学生,是个小商贩啊,这里肯定要做修改。这种情况下,我们很自然的会想到继承和多态。如果让小明这个学生和老婆婆这个小商贩从一个父类进行继承,那么我们只需要给小红牌超级计算器传入一个父类的引用就可以啦。

不过,实际使用中,考虑到java的单继承,以及不希望把自身太多东西暴漏给别人,这里使用从接口继承的方式配合内部类来做。

换句话说,小红希望以后继续向班里的小朋友们提供计算服务,同时还能向老婆婆提供算账服务,甚至以后能够拓展其他人的业务,于是她向所有的顾客约定了一个办法,用于统一的处理,也就是自己需要的操作数和做完计算之后应该怎么做。这个统一的方法,小红做成了一个接口,提供给了大家,代码如下:

public interface doJob
{
public void fillBlank(int a, int b, int result);
}
Copy after login

因为灵感来自帮小明填空,因此小红保留了初心,把所有业务都当做填空(fillBlank)来做。

同时,小红修改了自己的计算器,使其可以同时处理不同的实现了doJob接口的人,代码如下:

public class SuperCalculator
{
public void add(int a, int b, doJob customer)
{
int result = a + b;
customer.fillBlank(a, b, result);
}
}
Copy after login

小明和老婆婆拿到这个接口之后,只要实现了这个接口,就相当于按照统一的模式告诉小红得到结果之后的处理办法,按照之前说的使用内部类来做,代码如下:

小明的:

public class Student
{
private String name = null;
public Student(String name)
{
// TODO Auto-generated constructor stub
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
public class doHomeWork implements doJob
{
@Override
public void fillBlank(int a, int b, int result)
{
// TODO Auto-generated method stub
System.out.println(name + "求助小红计算:" + a + " + " + b + " = " + result);
}
}
public void callHelp (int a, int b)
{
new SuperCalculator().add(a, b, new doHomeWork());
}
}
Copy after login

老婆婆的:

public class Seller
{
private String name = null;
public Seller(String name)
{
// TODO Auto-generated constructor stub
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
public class doHomeWork implements doJob
{
@Override
public void fillBlank(int a, int b, int result)
{
// TODO Auto-generated method stub
System.out.println(name + "求助小红算账:" + a + " + " + b + " = " + result + "元");
}
}
public void callHelp (int a, int b)
{
new SuperCalculator().add(a, b, new doHomeWork());
}
}
Copy after login

测试程序如下:

public class Test
{
public static void main(String[] args)
{
int a = ;
int b = ;
int c = ;
int d = ;
Student s = new Student("小明");
Seller s = new Seller("老婆婆");
s.callHelp(a, b);
s.callHelp(c, d);
}
}
Copy after login

运行结果如下:

小明求助小红计算:56 + 31 = 87

老婆婆求助小红算账:26497 + 11256 = 37753元 

最后的话

可以很明显的看到,小红已经把这件事情当做一个事业来做了,看她给接口命的名字doJob就知道了。

更多详解 JAVA的回调机制CallBack相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template