通俗地解释回调函数
在以下场景中,假设A要给B发送消息,并且B收到消息后,需要告诉A我事情做完了。如果A不知道B到底有没有做完,就不能进行下一步操作,这时候我们就需要用到回调函数。
/**
* java的回调函数
*
* @author lewic
* @since 2020/4/29 20:29
*/
public class CallbackDemo {
public static void main(String[] args) {
// 测试
ASend aSend = new ASend();
aSend.sendB();
}
}
/**
* 发送方A
*/
class ASend {
/**
* 发送给B消息
*/
public void sendB() {
System.out.println("A send to new B, contain A object");
// 注意!!此处this为回调函数的精髓
new BReceived().receivedA(this);
}
/**
* 告诉我结果
*/
public boolean tellMe(boolean res) {
System.out.println("received result tell A by B");
return res;
}
}
/**
* 接收方B
*/
class BReceived {
private ASend aSend;
/**
* 接收A的请求
*/
public void receivedA(ASend aSend) {
System.out.println("B received A, set aSend");
this.aSend = aSend;
executeMy();
}
/**
* 接收到A请求后,B需要做的事
*/
private void executeMy() {
try {
System.out.println("exec my program");
finallyMy(true);
} catch (Exception e) {
finallyMy(false);
}
}
/**
* 事做完了,回调,通知A做的成功与否
*/
private void finallyMy(boolean res) {
System.out.println("B exec program, tell A result");
aSend.tellMe(res);
}
}
返回结果: A send to new B, contain A object B received A, set aSend exec my program B exec program, tell A result received result tell A by B
Process finished with exit code 0
此处,有个不足之处就是,当有多方C,D,E,F等等,都发送给B消息时,总不能在B中设置多个C,D,E,F吧。
此时,我们可以用接口或者抽象类完美解决这个问题。
/** * java的回调函数 * * @author lewic * @since 2020/4/29 20:29 */ public class CallbackDemo2 { public static void main(String[] args) { // 测试 ASend aSend = new ASend(); aSend.sendB(); System.out.println(); CSend cSend = new CSend(); cSend.sendB(); } } interface SendCallback { boolean tellMe(boolean res); } /** * 发送方A */ class ASend implements SendCallback{ /** * 发送给B消息 */ public void sendB() { System.out.println("A send to new B, contain A object"); new BReceived().receivedA(this); } /** * 告诉A结果 */ @Override public boolean tellMe(boolean res) { System.out.println("received result tell A by B"); return res; } } /** * 发送方C */ class CSend implements SendCallback{ /** * 发送给B消息 */ public void sendB() { System.out.println("C send to new B, contain C object"); new BReceived().receivedA(this); } /** * 告诉C结果 */ @Override public boolean tellMe(boolean res) { System.out.println("received result tell C by B"); return res; } } /** * 接收方B */ class BReceived { private SendCallback send; /** * 接收A的请求 */ public void receivedA(SendCallback send) { System.out.println("B received A, set aSend"); this.send = send; executeMy(); } /** * 接收到A请求后,B需要做的事 */ private void executeMy() { try { System.out.println("exec my program"); finallyMy(true); } catch (Exception e) { finallyMy(false); } } /** * 事做完了,回调,通知Send做的成功与否 */ private void finallyMy(boolean res) { System.out.println("B exec program, tell Send result"); send.tellMe(res); } }
结果如下: A send to new B, contain A object B received A, set aSend exec my program B exec program, tell Send result received result tell A by B C send to new B, contain C object B received A, set aSend exec my program B exec program, tell Send result received result tell C by B Process finished with exit code 0