JavaBean之Builder模式
https://blog.csdn.net/kangkanglou/article/details/78724240
靜態工廠和構造器
過載建構函式,分別包含多個不同的引數
無法很好地擴充套件到大量的可選引數
JavaBean方式
呼叫一個無參建構函式來建立物件,然後通過setter方法來設定必要引數
物件構造過程被分割到了幾個呼叫,構造過程中可能導致JavaBean處於不一致的狀態,類無法通過檢驗構造器引數的有效性來保證一致性
Builder模式
不直接生成想要的物件,通過呼叫構造器(或靜態工廠),得到builder物件,客戶端在builder物件上呼叫類似setter方法,來設定每個相關的可選引數,最後,再呼叫build方法來生成不可變物件
public class Person {
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
private String name;
private String address;
private String firstName;
private String lastName;
private Person(String name, String address, String firstName, String lastName) {
this.address = address;
this.name = name;
this.lastName = lastName;
this.firstName = firstName;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String name;
private String address;
private String firstName;
private String lastName;
public Builder name(final String name) {
this.name = name;
return this;
}
public Builder address(final String address) {
this.address = address;
return this;
}
public Builder lastName(final String lastName) {
this.lastName = lastName;
return this;
}
public Builder firstName(final String firstName) {
this.firstName = firstName;
return this;
}
public Person build() {
return new Person(name, address, firstName, lastName);
}
}
}
客戶端呼叫
Person p = new Person.Builder().address("上海").firstName("三").lastName("張").name("歐陽").build();
System.out.println(p);
Person person = Person.builder().address("上海").firstName("三").lastName("張").name("歐陽").build();
System.out.println(person);
如果我們使用IDE,我們可以直接使用Lombok外掛的@Builder註解,僅需宣告類屬性
@Builder
@ToString
public class LombokPerson {
private String name;
private String address;
private String firstName;
private String lastName;
}
檢視生成的class原始碼
public class LombokPerson {
private String name;
private String address;
private String firstName;
private String lastName;
@ConstructorProperties({"name", "address", "firstName", "lastName"})
LombokPerson(String name, String address, String firstName, String lastName) {
this.name = name;
this.address = address;
this.firstName = firstName;
this.lastName = lastName;
}
public static LombokPerson.LombokPersonBuilder builder() {
return new LombokPerson.LombokPersonBuilder();
}
public String toString() {
return "LombokPerson(name=" + this.name + ", address=" + this.address + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ")";
}
public static class LombokPersonBuilder {
private String name;
private String address;
private String firstName;
private String lastName;
LombokPersonBuilder() {
}
public LombokPerson.LombokPersonBuilder name(String name) {
this.name = name;
return this;
}
public LombokPerson.LombokPersonBuilder address(String address) {
this.address = address;
return this;
}
public LombokPerson.LombokPersonBuilder firstName(String firstName) {
this.firstName = firstName;
return this;
}
public LombokPerson.LombokPersonBuilder lastName(String lastName) {
this.lastName = lastName;
return this;
}
public LombokPerson build() {
return new LombokPerson(this.name, this.address, this.firstName, this.lastName);
}
public String toString() {
return "LombokPerson.LombokPersonBuilder(name=" + this.name + ", address=" + this.address + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ")";
}
}
}
客戶端呼叫:
LombokPerson lombokPerson = LombokPerson.builder().address("上海").firstName("三").lastName("張").name("歐陽").build();
System.out.println(lombokPerson);
相關文章
- Java設計模式之builder模式Java設計模式UI
- 設計模式之-建造者模式-Builder設計模式UI
- 設計模式Builder之惑設計模式UI
- Java設計模式之建造者模式(Builder)Java設計模式UI
- JAVA設計模式之 建造者模式【Builder Pattern】Java設計模式UI
- Builder 模式UI模式
- Builder模式UI模式
- Java 高效程式設計之 Builder 模式Java程式設計UI模式
- java靈活傳參之builder模式JavaUI模式
- 設計模式實戰系列之@Builder和建造者模式設計模式UI
- 設計模式:Builder模式概述設計模式UI
- 建立模式 03-Builder模式模式UI
- 模式一:jsp+javaBean模式JSJavaBean
- 建造者模式(Builder)模式UI
- Builder設計模式UI設計模式
- 詳解 - Builder模式UI模式
- Builder模式的理解!!UI模式
- 設計模式-建造者模式(Builder)設計模式UI
- 生成器模式(Builder)模式UI
- 建造者模式(Builder Pattern)模式UI
- [用設計模式鍛鍊C#系列]之Builder (轉)設計模式C#UI
- 設計模式-生成器模式Builder設計模式UI
- 【設計模式】- 生成器模式(Builder)設計模式UI
- 設計模式--建造者模式(Builder Pattern)設計模式UI
- 【設計模式筆記】(二)- Builder模式設計模式筆記UI
- Java設計模式----建造者模式(Builder)Java設計模式UI
- Builder模式,今天你用了嘛UI模式
- Builder模式與Java語法UI模式Java
- 深入瞭解 Builder 模式 - frankelUI模式
- 構建者模式(Builder pattern)模式UI
- 設計模式(五)Builder構建者模式設計模式UI
- Android設計模式探討--Builder模式Android設計模式UI
- 《設計模式》 - 3. 建立者模式( Builder )設計模式UI
- Android設計模式探討 Builder模式Android設計模式UI
- 設計模式--建造者模式Builder(建立型)設計模式UI
- Jsp+JavaBean模式,Jsp+Servlet模式,MVC模式介紹JSJavaBean模式ServletMVC
- 設計模式系列之建造者模式(Builder Pattern)——複雜物件的組裝與建立設計模式UI物件
- 重識設計模式-建造者模式(Builder Pattern)設計模式UI