Java类的Builder应用以及使用@Data和@Builder高效应用Builder

记录:395

场景:手动编写Java类的Builder及其应用。使用lombok的@Data和@Builder注解构建Java类的Builder及其应用。

Java Builder模式:是Java设计模式之一,它属于对象创建型模式,是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

结论一:使用lombok的@Data和@Builder注解构建Java类的Builder简洁高效,推荐实际应用。

结论二:手动编写Java类的Builder,能够全貌理解Builder模式,推荐试试。

结论三:Java类的Builder模式,最大好处就是构建对象时,可以灵活构建需要的属性。

7.1普通Java类

7.1.1普通Java对象

普通Java类,一般有属性、set、get、toString方法。

7.1.2代码

public class City01Response implements Serializable {
  private boolean success;
  private String msg;
  private Long cityId;
  private String cityName;
  private String cityDescribe;
  public City01Response() {
  }
  public boolean isSuccess() {
      return success;
  }
  public void setSuccess(boolean success) {
      this.success = success;
  }
  public String getMsg() {
      return msg;
  }
  public void setMsg(String msg) {
      this.msg = msg;
  }
  public Long getCityId() {
      return cityId;
  }
  public void setCityId(Long cityId) {
      this.cityId = cityId;
  }
  public String getCityName() {
      return cityName;
  }
  public void setCityName(String cityName) {
      this.cityName = cityName;
  }
  public String getCityDescribe() {
      return cityDescribe;
  }
  public void setCityDescribe(String cityDescribe) {
      this.cityDescribe = cityDescribe;
  }
  @Override
  public String toString() {
      return "City01Response(" + "success=" + success + ", msg=" + msg + ", cityId=" + cityId + ", cityName=" + cityName + ", cityDescribe=" + cityDescribe + ")";
  }
}

7.2普通Java类使用@Data注解

7.2.1普通Java类

使用@Data注解后,普通Java类,只有属性。set、get、toString方法由@Data注解接管。使用方式等价。

7.2.2代码

@Data
public class City01Response implements Serializable {
  private boolean success;
  private String msg;
  private Long cityId;
  private String cityName;
  private String cityDescribe;
}

7.3手动编写Java类的Builder(静态内部类)

7.3.1手动编写Java类的Builder

本例内部类使用静态类实现,build使用静态方法。

普通Java类:City02Response

(1)编写普通Java类:City02Response,一般有属性、set、get、toString方法。

(2)在普通Java类:City02Response中,编写一个内部类:City02ResponseBuilder。

(3)在普通Java类:City02Response中,实现一个builder方法,创建一个内部类City02ResponseBuilder的对象。

(4)在内部类:City02ResponseBuilder中,实现一个build方法,返回普通Java类:City02Response的对象。

(5)在内部类:City02ResponseBuilder中。为每个属性实现一个设置方法,并且返回City02ResponseBuilder对象本身,即返回this。

7.3.2代码

public class City02Response implements Serializable {
  private boolean success;
  private String msg;
  private Long cityId;
  private String cityName;
  private String cityDescribe;
  public static City02Response.City02ResponseBuilder builder() {
      return new City02Response.City02ResponseBuilder();
  }
  public static City02Response getCityInfoSuccess(Long cityID, String cityName, String cityDescribe) {
      return builder().success(true).msg("获取城市信息成功").cityId(cityID).cityName(cityName).cityDescribe(cityDescribe).build();
  }
  public static City02Response getCityInfoFail(Long cityID, String failMsg) {
      return builder().success(false).msg(failMsg).cityId(cityID).build();
  }
  public City02Response() {
  }
  public City02Response(boolean success, String msg, Long cityId, String cityName, String cityDescribe) {
      this.success = success;
      this.msg = msg;
      this.cityId = cityId;
      this.cityName = cityName;
      this.cityDescribe = cityDescribe;
  }
  @Override
  public String toString() {
      return "City02Response(" + "success=" + success + ", msg=" + msg + ", cityId=" + cityId + ", cityName=" + cityName + ", cityDescribe=" + cityDescribe + ")";
  }
  public boolean isSuccess() {
      return success;
  }
  public void setSuccess(boolean success) {
      this.success = success;
  }
  public String getMsg() {
      return msg;
  }
  public void setMsg(String msg) {
      this.msg = msg;
  }
  public Long getCityId() {
      return cityId;
  }
  public void setCityId(Long cityId) {
      this.cityId = cityId;
  }
  public String getCityName() {
      return cityName;
  }
  public void setCityName(String cityName) {
      this.cityName = cityName;
  }
  public String getCityDescribe() {
      return cityDescribe;
  }
  public void setCityDescribe(String cityDescribe) {
      this.cityDescribe = cityDescribe;
  }
  public static class City02ResponseBuilder {
    private boolean success;
    private String msg;
    private Long cityId;
    private String cityName;
    private String cityDescribe;
    City02ResponseBuilder() {
    }
    public City02Response build() {
        return new City02Response(success, msg, cityId, cityName, cityDescribe);
    }
    public City02Response.City02ResponseBuilder success(final boolean success) {
        this.success = success;
        return this;
    }
    public City02Response.City02ResponseBuilder msg(final String msg) {
        this.msg = msg;
        return this;
    }
    public City02Response.City02ResponseBuilder cityId(final Long cityId) {
        this.cityId = cityId;
        return this;
    }
    public City02Response.City02ResponseBuilder cityName(final String cityName) {
        this.cityName = cityName;
        return this;
    }
    public City02Response.City02ResponseBuilder cityDescribe(final String cityDescribe) {
        this.cityDescribe = cityDescribe;
        return this;
    }
    @Override
    public String toString() {
        return "City02ResponseBuilder.City02Response(" + "success=" + success + ", msg=" + msg + ", cityId=" + cityId + ", cityName=" + cityName + ", cityDescribe=" + cityDescribe + ")";
    }
  }
}

7.3.3代码中的getCityInfoSuccess和getCityInfoFail是对builder封装使用

代码中的getCityInfoSuccess和getCityInfoFail是对builder封装应用。不是必须部分。

7.4使用lombok的@Data和@Builder注解构建Java对象的Builder

7.4.1使用@Data和@Builder注解

使用@Data和@Builder注解,简化代码,效果等价。需引入jar包。


  org.projectlombok
  lombok
  1.18.24

7.4.2代码

@Data
@Builder
public class City03Response implements Serializable {
  private boolean success;
  private String msg;
  private Long cityId;
  private String cityName;
  private String cityDescribe;
  public static City03Response getCityInfoSuccess(Long cityID, String cityName, String cityDescribe) {
    return builder().success(true).msg("获取城市信息成功").cityId(cityID).cityName(cityName).cityDescribe(cityDescribe).build();
  }
  public static City03Response getCityInfoFail(Long cityID, String failMsg) {
    return builder().success(false).msg(failMsg).cityId(cityID).build();
  }
}

7.4.3代码中的getCityInfoSuccess和getCityInfoFail是对builder封装使用

代码中的getCityInfoSuccess和getCityInfoFail是对builder封装应用。不是必须部分。

7.5普通Jave类和Builder类分开

7.5.1普通Java类

public class City04Response {
  private boolean success;
  private String msg;
  private Long cityId;
  private String cityName;
  private String cityDescribe;
  public City04Response(boolean success, String msg, Long cityId, String cityName, String cityDescribe) {
    this.success = success;
    this.msg = msg;
    this.cityId = cityId;
    this.cityName = cityName;
    this.cityDescribe = cityDescribe;
  }
  public boolean isSuccess() {
    return success;
  }
  public void setSuccess(boolean success) {
    this.success = success;
  }
  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
  public Long getCityId() {
    return cityId;
  }
  public void setCityId(Long cityId) {
    this.cityId = cityId;
  }
  public String getCityName() {
    return cityName;
  }
  public void setCityName(String cityName) {
    this.cityName = cityName;
  }
  public String getCityDescribe() {
    return cityDescribe;
  }
  public void setCityDescribe(String cityDescribe) {
    this.cityDescribe = cityDescribe;
  }
  @Override
  public String toString() {
    return "City04Response(" + "success=" + success + ", msg=" + msg + ", cityId=" + cityId + ", cityName=" + cityName + ", cityDescribe=" + cityDescribe + ")";
  }
}

7.5.2普通Java类的Builder类

public class City04ResponseBuilder implements Serializable {
  private boolean success;
  private String msg;
  private Long cityId;
  private String cityName;
  private String cityDescribe;
  public City04ResponseBuilder setSuccess(boolean success) {
    this.success = success;
    return this;
  }
  public City04ResponseBuilder setMsg(String msg) {
    this.msg = msg;
    return this;
  }
  public City04ResponseBuilder setCityId(Long cityId) {
    this.cityId = cityId;
    return this;
  }
  public City04ResponseBuilder setCityName(String cityName) {
    this.cityName = cityName;
    return this;
  }
  public City04ResponseBuilder setCityDescribe(String cityDescribe) {
    this.cityDescribe = cityDescribe;
    return this;
  }
  public City04Response build() {
    return new City04Response(success, msg, cityId, cityName, cityDescribe);
  }
}

7.6应用

7.6.1测试

public class UseBuildUtils {
  public static void main(String[] args) {
    City02Response city02Response;
    System.out.println("原生的Builder的Java对象,City02Response的Builder使用:");
    city02Response = City02Response.builder().success(true).msg("获取城市信息成功").cityId(20230328L).cityName("杭州").cityDescribe("杭州是一个互联网城市").build();
    System.out.println("City02Response直接使用builder: " +city02Response.toString());
    city02Response = City02Response.getCityInfoSuccess(20230328L, "杭州", "杭州是一个互联网城市");
    System.out.println("City02Response获取成功: " +city02Response.toString());
    city02Response = City02Response.getCityInfoFail(20230328L, "网络异常,无法获取信息");
    System.out.println("City02Response获取失败: " +city02Response.toString());
    City03Response city03Response;
    System.out.println("基于注解@Data和@Builder的Java对象,city03Response的Builder使用:");
    city03Response = City03Response.builder().success(true).msg("获取城市信息成功").cityId(20230328L).cityName("苏州").cityDescribe("工业城市").build();
    System.out.println("city03Response直接使用builder: " +city03Response.toString());
    city03Response = City03Response.getCityInfoSuccess(20230328L, "苏州", "工业城市");
    System.out.println("city03Response获取成功: " +city03Response.toString());
    city03Response = City03Response.getCityInfoFail(20230328L, "网络异常,无法获取信息");
    System.out.println("city03Response获取失败: " +city03Response.toString());
    System.out.println("普通Java类和Builder类分开场景的Builder使用:");
    City04Response city04Response = new City04ResponseBuilder().setSuccess(true).setMsg("获取城市信息成功").setCityId(20230328L).setCityName("宁波").setCityDescribe("港口城市").build();
    System.out.println("city04Response获取成功:"+city04Response.toString());
  }
}

7.6.2打印

原生的Builder的Java对象,City02Response的Builder使用:
City02Response直接使用builder: City02Response(success=true, msg=获取城市信息成功, cityId=20230328, cityName=杭州, cityDescribe=杭州是一个互联网城市)
City02Response获取成功: City02Response(success=true, msg=获取城市信息成功, cityId=20230328, cityName=杭州, cityDescribe=杭州是一个互联网城市)
City02Response获取失败: City02Response(success=false, msg=网络异常,无法获取信息, cityId=20230328, cityName=null, cityDescribe=null)
基于注解@Data和@Builder的Java对象,city03Response的Builder使用:
city03Response直接使用builder: City03Response(success=true, msg=获取城市信息成功, cityId=20230328, cityName=苏州, cityDescribe=工业城市)
city03Response获取成功: City03Response(success=true, msg=获取城市信息成功, cityId=20230328, cityName=苏州, cityDescribe=工业城市)
city03Response获取失败: City03Response(success=false, msg=网络异常,无法获取信息, cityId=20230328, cityName=null, cityDescribe=null)
普通Java类和Builder类分开场景的Builder使用:
city04Response获取成功:City04Response(success=true, msg=获取城市信息成功, cityId=20230328, cityName=宁波, cityDescribe=港口城市)

以上,感谢。

2023年3月29日

你可能感兴趣的:(L02-Java基础,java,builder,构建模式,build,内部类)