Builder設計模式結合lombok減少過多傳參

OkidoGreen發表於2020-04-05

https://blog.csdn.net/j16421881/article/details/79368004

建造者模式將複雜物件的建立過程簡化,用來傳參也很合適。lombok的主要作用是通過一些註解,消除樣板式程式碼,更多詳見 lombok官網。該框架已經內建了這種模式。

  過多的傳參,可讀性差,尤其是引數型別一致順序還容易寫顛倒。下面以一則不友好的傳參為例展示一下,如何通過lombok迅速重構。

public class Mytest {
  public static void main(String[] args) {
    pay("2018-02-25", "小明", "2", "3");
  }
  public static void pay(String date, String name, String term, String amount) {
    System.out.println(date + "日,借款人" + name + "在第" + term + "期還款" + amount);
  }
}

首先將引數封裝。 
Builder跟Date分別是兩個聚合註解,省去了手寫getter、setter跟builder方法。

import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class BidBuilder {
  private String date;
  private String name;
  private String term;
  private String amount;
}

加上該註解後,通過反編譯發現lombok生成的實際程式碼如下

public class BidBuilder
{
  private String date;
  private String name;
  private String term;
  private String amount;

  public void setDate(String date)
  {
    this.date = date;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public void setTerm(String term)
  {
    this.term = term;
  }

  public void setAmount(String amount)
  {
    this.amount = amount;
  }

  public boolean equals(Object o)
  {
    if (o == this) {
      return true;
    }
    if (!(o instanceof BidBuilder)) {
      return false;
    }
    BidBuilder other = (BidBuilder)o;
    if (!other.canEqual(this)) {
      return false;
    }
    Object this$date = getDate();Object other$date = other.getDate();
    if (this$date == null ? other$date != null : !this$date.equals(other$date)) {
      return false;
    }
    Object this$name = getName();Object other$name = other.getName();
    if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
      return false;
    }
    Object this$term = getTerm();Object other$term = other.getTerm();
    if (this$term == null ? other$term != null : !this$term.equals(other$term)) {
      return false;
    }
    Object this$amount = getAmount();Object other$amount = other.getAmount();return this$amount == null ? other$amount == null : this$amount.equals(other$amount);
  }

  protected boolean canEqual(Object other)
  {
    return other instanceof BidBuilder;
  }

  public int hashCode()
  {
    int PRIME = 59;int result = 1;Object $date = getDate();result = result * 59 + ($date == null ? 43 : $date.hashCode());Object $name = getName();result = result * 59 + ($name == null ? 43 : $name.hashCode());Object $term = getTerm();result = result * 59 + ($term == null ? 43 : $term.hashCode());Object $amount = getAmount();result = result * 59 + ($amount == null ? 43 : $amount.hashCode());return result;
  }

  public String toString()
  {
    return "BidBuilder(date=" + getDate() + ", name=" + getName() + ", term=" + getTerm() + ", amount=" + getAmount() + ")";
  }

  public static class BidBuilderBuilder
  {
    private String date;
    private String name;
    private String term;
    private String amount;

    public String toString()
    {
      return "BidBuilder.BidBuilderBuilder(date=" + this.date + ", name=" + this.name + ", term=" + this.term + ", amount=" + this.amount + ")";
    }

    public BidBuilder build()
    {
      return new BidBuilder(this.date, this.name, this.term, this.amount);
    }

    public BidBuilderBuilder amount(String amount)
    {
      this.amount = amount;return this;
    }

    public BidBuilderBuilder term(String term)
    {
      this.term = term;return this;
    }

    public BidBuilderBuilder name(String name)
    {
      this.name = name;return this;
    }

    public BidBuilderBuilder date(String date)
    {
      this.date = date;return this;
    }
  }

  BidBuilder(String date, String name, String term, String amount)
  {
    this.date = date;this.name = name;this.term = term;this.amount = amount;
  }

  public static BidBuilder.BidBuilderBuilder builder()
  {
    return new BidBuilder.BidBuilderBuilder();
  }

  public String getDate()
  {
    return this.date;
  }

  public String getName()
  {
    return this.name;
  }

  public String getTerm()
  {
    return this.term;
  }

  public String getAmount()
  {
    return this.amount;
  }
}

通過lombok幾行程式碼就實現了大量需要手動完成的模版程式碼。重構後如下

public class Mytest2 {
  public static void main(String[] args) {
    BidBuilder bidBuilder =
        BidBuilder.builder().date("2018-02-25").name("小明").term("2").amount("3").build();
    pay(bidBuilder);
  }

  public static void pay(BidBuilder bidBuilder) {
    System.out.println(
        bidBuilder.getDate()
            + "日,借款人"
            + bidBuilder.getName()
            + "在第"
            + bidBuilder.getTerm()
            + "期還款"
            + bidBuilder.getAmount());
  }
}

後續若需要新增新的引數,只需更改BidBuilder,比如新增下次還款時間,只需要新增一行.

@Data
@Builder
public class BidBuilder {
  // 篇幅限制,省略其他引數
  // 下次還款時間 
  private String nextDate;
}
 // 然後動態生成新的Builder
 BidBuilder bidBuilder =
        BidBuilder.builder()
            .date("2018-02-25")
            .name("小明")
            .term("2")
            .amount("100")
            .nextDate("2018-03-25")
            .build();

 

相關文章