用Build來構建對象的寫法

若是一個類的屬性過多,用構造器來構建對象很難寫,所以咱們時用Build方式來構建對象。寫法大體以下。java

import java.io.Serializable;
import java.util.Date;

public class Log implements Serializable {
   private static final long serialVersionUID = 3355538580546591073L;

   private Long id;
   private String username;
   /** 模塊 */
   private String module;
   /** 參數值 */
   private String params;
   private String remark;
   private Boolean flag;
   private Date createTime;

   public Log(Builder builder) {
      this.id = builder.id;
      this.username = builder.username;
      this.module = builder.module;
      this.params = builder.params;
      this.remark = builder.remark;
      this.flag = builder.flag;
      this.createTime = builder.createTime;
   }
   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getModule() {
      return module;
   }

   public void setModule(String module) {
      this.module = module;
   }

   public String getParams() {
      return params;
   }

   public void setParams(String params) {
      this.params = params;
   }

   public String getRemark() {
      return remark;
   }

   public void setRemark(String remark) {
      this.remark = remark;
   }

   public Boolean getFlag() {
      return flag;
   }

   public void setFlag(Boolean flag) {
      this.flag = flag;
   }

   public Date getCreateTime() {
      return createTime;
   }

   public void setCreateTime(Date createTime) {
      this.createTime = createTime;
   }
   public static class Builder {
      private static Long id;
      private static String username;
      /** 模塊 */
      private static String module;
      /** 參數值 */
      private static String params;
      private static String remark;
      private static Boolean flag;
      private static Date createTime;
      public Builder() {

      }
      public Builder id(Long id) {
         this.id = id;
         return this;
      }
      public Builder username(String username) {
         this.username = username;
         return this;
      }
      public Builder module(String module) {
         this.module = module;
         return this;
      }
      public Builder params(String params) {
         this.params = params;
         return this;
      }
      public Builder remark(String remark) {
         this.remark = remark;
         return this;
      }
      public Builder flag(Boolean flag) {
         this.flag = flag;
         return this;
      }
      public Builder createTime(Date createTime) {
         this.createTime = createTime;
         return this;
      }
      public Log build() {
         return new Log(this);
      }
   }
}

調用方法:ui

Log log =new Log.Builder().username(username).module(LogModule.LOGIN).remark(remark).createTime(new Date())
        .build();

這樣咱們就能夠時用咱們有須要的屬性賦值,而且進行構建對象。this

相關文章
相關標籤/搜索