Java将方法作为参数传递
2020-04-03 本文已影响0人
佑岷
最近在用Java写一套自研爬虫框架,该框架可以通过简单的配置就可以实现目标网站的抓取,不再需要研发人员做开发,业务人员也能抓目标网站哦,有兴趣可以私聊。
为了抽象化需要将用户自定义方法传递给框架,供框架回调。
研究了下发现1.8版新特效lambda表达式配合java.util.function.Function<T, R>可以实现。
java.util.function.Function<T, R>中的T、R,需要结合这个interface的方法R apply(T t)来看, T是输入参数类型,R是返回参数类型。
流程:将自定义方法传递到Request对象,当request对象获取response后,回调自定义方法。
实例分析:
import java.util.function.Function;
public class Cb {
public static class Response {
private String html;
public Response(String html) { this.html = html; }
public String getHtml() { return html; }
}
public static class Request {
private Function<Response, Object> callback;
public Request(Function<Response, Object> callback) { this.callback = callback; }
public Function<Response, Object> getCallback() { return callback; }
}
public static class Engine {
private Request request;
public void setRequest(Request request) { this.request = request; }
public void run() {
Object o = request.getCallback().apply(new Response("I like moving, moving"));
System.out.println(o);
}
}
public static void main(String[] args) {
Engine engine = new Engine();
engine.setRequest(new Request(response -> returnHtml(response))); engine.run();
engine.setRequest(new Request(response -> returnHtmlb(response))); engine.run();
engine.setRequest(new Request(response -> returnHtmlc(response))); engine.run();
}
public static Object returnHtml(Response response) { return response.getHtml(); }
public static Object returnHtmlb(Response response) { return response.getHtml() + " bb"; }
public static Object returnHtmlc(Response response) { return response.getHtml() + " ccc"; }
}
Lambda搭配Function<T, R>调用时的写法response -> selfFunction(response);
Ok~