回调模式
@Slf4j
public class CallBack {
/**
* 回调模式更适合于函数式编程语言。
*/
@Test
public void all() {
final SimpleTask task = new SimpleTask();
task.execute(r -> {
log.info("do callback");
assertEquals("hello", r);
});
}
}
// 1)定义回调接口
interface ICallBack {
void call(String t);
}
@Data
abstract class Task {
private String result;
public void execute(ICallBack callBack) {
dowork();
callBack.call(result);
}
protected abstract void dowork();
}
@Slf4j
class SimpleTask extends Task {
@Override
protected void dowork() {
log.info("dowork now");
setResult("hello");
}
}
原文地址:https://www.cnblogs.com/zhuxudong/p/10170825.html
时间: 2024-10-08 07:32:05