Build构建者模式,如何使用泛型
public class Result implements Serializable {
@ApiModelProperty(value = “状态码,0表示成功 其他表示失败”, example = “0”)
private int status;
@ApiModelProperty(value = “提示信息”, example = “ADS_SUCCESS”)
private String message = “”;
@ApiModelProperty(value = “数据”, example = “”)
private T data;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Result() {
}
private Result(int status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
public static Result.ResultBuiler builder(){
return new Result.ResultBuiler();
}
public static class ResultBuiler{
private int status;
private String message;
private T data;
public Result.ResultBuiler status(int status){
this.status = status;
return this;
}
public Result.ResultBuiler message(String message){
this.message = message;
return this;
}
public Result.ResultBuiler data(T data){
this.data = data;
return this;
}
public Result build(){
return new Result(this.status,this.message,this.data);
}
}
}
如何调用呢?直接上代码
Result <User> result;
result = Result.<User>builder().status(200).message(“success”).data(user).build();