Google 上搜索”java链式调用”前两名的大哥用的例子是一样的,并且不正确.
他们代码中build
出来的并不是Student
,而是一个Builder
.
这里的链式调用(链式编程),是设计模式中的Builder模式
,要点就是通过一个代理来完成对象的构建过程。这个代理职责就是完成构建的各个步骤,同时它也是易扩展的。
如下类似代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| public class Response {
private final Integer status;
private final String message;
private final Object result;
public Response(Builder builder) {
this.status=builder.status;
this.message=builder.message;
this.result=builder.result;
}
public static class Builder{
private Integer status;
private String message;
private Object result;
public Response build(){
return new Response(this);
}
public Builder withStatus(Integer status){
this.status=status;
return this;
}
public Builder withMessage(String message){
this.message = message;
return this;
}
public Builder withResult(Object result){
this.result = result;
return this;
}
}
}
|
调用时如下方式:
1
2
3
4
5
| Response response = new Response.Builder()
.withStatus(1)
.withMessage("请求成功")
.withResult(XXX)
.build();
|