回调
代码仓库:https://github.com/hedsay/Java_Scavenging
回调:通过函数参数传递到 其它代码中某一块可执行代码的引用;
# 需求
主函数中,异步发送一个请求,指定处理响应的回调函数,接着主函数去做其它事,当响应到达后,执行回调函数;
# 反射
Request类中,以Method类型去获取回调函数,然后传递给请求函数;
public class Request {
public void send(Class clazz, Method method) throws Exception{
Thread.sleep(3000);
System.out.println("[Request]:收到响应");
method.invoke(clazz.newInstance());
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
CallBack类中的processResponse方法当作回调函数;
public class CallBack {
public void processResponse(){
System.out.println("[CallBack]:处理响应");
}
}
1
2
3
4
5
2
3
4
5
main方法中,新开一个线程发送请求,将需要回调的类和方法传递进去;
public static void main(String[] args) throws Exception {
Request request = new Request();
System.out.println("[Main]:我开个线程去异步发请求");
new Thread(() -> {
try {
request.send(CallBack.class, CallBack.class.getMethod("processResponse"));
} catch (Exception e) {
e.printStackTrace();
}
}).start();
System.out.println("[Main]:请求发完了,我去干点别的");
Thread.sleep(100000);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 直接调用
Request类中send方法不使用反射,直接通过对象调用方法;
public class Request {
public void send(CallBack callBack) throws Exception {
// 模拟等待响应
Thread.sleep(3000);
System.out.println("[Request]:收到响应");
callBack.processResponse();
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
主方法中直接传递 CallBack对象;
public static void main(String[] args) throws Exception {
Request request = new Request();
System.out.println("[Main]:我开个线程去异步发请求");
CallBack callBack = new CallBack();
new Thread(() -> {
try {
request.send(callBack);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
System.out.println("[Main]:请求发完了,我去干点别的");
Thread.sleep(100000);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
缺点:不符合开闭原则,当需要修改一种”处理响应“的方法,必须用修改CallBack类中的processResponse()方法;
# 接口调用
将CallBack类改为接口,修改处理响应方法只需要改变CallBack的实现;
public interface CallBack {
public void processResponse();
}
1
2
3
2
3
public class CallBackImpl implements CallBack {
@Override
public void processResponse() {
System.out.println("[CallBack]:处理响应");
}
}
1
2
3
4
5
6
2
3
4
5
6
Request类中还是传递CallBack接口;
public class Request {
public void send(CallBack callBack) throws Exception {
// 模拟等待响应
Thread.sleep(3000);
System.out.println("[Request]:收到响应");
callBack.processResponse();
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
主函数:
public static void main(String[] args) throws Exception {
Request request = new Request();
System.out.println("[Main]:我开个线程去异步发请求");
CallBack callBack = new CallBackImpl();
new Thread(() -> {
try {
request.send(callBack);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
System.out.println("[Main]:请求发完了,我去干点别的");
Thread.sleep(100000);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# Lambda表达式
直接在主函数中写实现类;
public static void main(String[] args) throws Exception {
Request request = new Request();
System.out.println("[Main]:我开个线程去异步发请求");
new Thread(() -> {
try {
request.send(()-> System.out.println("[CallBack]:处理响应"));
} catch (Exception e) {
e.printStackTrace();
}
}).start();
System.out.println("[Main]:请求发完了,我去干点别的");
Thread.sleep(100000);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13