# 使用静态方法代替构造器创建实例
# 简介
在一些业务场景下,我们为了程序灵活性,需要通过构造多个参数的构造器来创建实例。由于参数的多样性,会给创建者带来一定的困惑,这种情况下可以考虑使用静态方法来创建实例。
# 示例
public class Result<T> {
//业务结果标识
private boolean flag;
//http 状态码
private int code;
//业务结果消息
private String msg;
//业务数据
private T data;
public Result(){
this.flag = true;
this.code = 200;
this.msg = "success";
}
public Result(boolean flag, String msg){
this.flag = flag;
this.msg = msg;
this.code = 200;
}
public Result(boolean flag, int code, String msg){
this.flag = flag;
this.code = code;
this.msg = msg;
}
public Result(boolean flag, int code, String msg, T data){
this.flag = flag;
this.code = code;
this.msg = msg;
this.data = data;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "Result{" +
"flag=" + flag +
", code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
# 调用
@Test
public void tt(){
//业务处理成功,并返回业务数据
Result result = new Result(true, 200, "success", "ZhangSan");
System.out.println(result);
//处理成功,无业务数据
result = new Result();
System.out.println(result);
//处理失败,无业务数据
result = new Result(false, "handle faild");
System.out.println(result);
//系统级http错误码
result = new Result(false, 400, "Bad Request");
System.out.println(result);
}
# 使用静态方法创建实例
public class Result<T> {
//业务结果标识
private boolean flag;
//http 状态码
private int code;
//业务结果消息
private String msg;
//业务数据
private T data;
private Result(){
this.flag = true;
this.code = 200;
this.msg = "success";
}
private Result(boolean flag, String msg){
this.flag = flag;
this.msg = msg;
this.code = 200;
}
private Result(boolean flag, int code, String msg){
this.flag = flag;
this.code = code;
this.msg = msg;
}
private Result(boolean flag, int code, String msg, T data){
this.flag = flag;
this.code = code;
this.msg = msg;
this.data = data;
}
public static <T> Result okAndData(T data){
return new Result(true, 200, "success", data);
}
public static Result ok(){
return new Result();
}
public static Result faild(){
return new Result(false, 200, "faild", null);
}
public static Result faildAndMsg(String msg){
return new Result(false, 200, msg, null);
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "Result{" +
"flag=" + flag +
", code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
# 调用
@Test
public void tt2(){
Result result = Result.ok();
System.out.println(result);
result = Result.okAndData("ZhangSan");
System.out.println(result);
result = Result.faild();
System.out.println(result);
result = Result.faildAndMsg("server handle faild");
System.out.println(result);
}
# 使用静态方法创建实例好处
- 让创建实例者很清楚的知道要创建的是一个什么功能的实例。
- 可以不必每次都new一个新的实例,你可以自由控制实例个数,比如上面的demo中,针对Result.ok(),可以每次只创建一次。或者如jdk源码中的Integer.valueOf方法,会提前缓存好-128到127的Integer对象,在这个数字范围内的实例,其实是复用的。
- 可以创建一个当前class的子类型实例,类似简单工厂模式,你可以根据不同的参数来返回不同的子类对象。