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模式
- java靈活傳參之builder模式JavaUI模式
- Java 高效程式設計之 Builder 模式Java程式設計UI模式
- 設計模式實戰系列之@Builder和建造者模式設計模式UI
- 建立模式 03-Builder模式模式UI
- 設計模式:Builder模式概述設計模式UI
- 建造者模式(Builder)模式UI
- Jsp+JavaBean模式,Jsp+Servlet模式,MVC模式介紹JSJavaBean模式ServletMVC
- 設計模式-建造者模式(Builder)設計模式UI
- 建造者模式(Builder Pattern)模式UI
- 生成器模式(Builder)模式UI
- 設計模式--建造者模式(Builder Pattern)設計模式UI
- 【設計模式筆記】(二)- Builder模式設計模式筆記UI
- 設計模式-生成器模式Builder設計模式UI
- 【設計模式】- 生成器模式(Builder)設計模式UI
- JavaWeb——JSP呼叫資料庫、JavaBean、MVC模式JavaWebJS資料庫BeanMVC模式
- 構建者模式(Builder pattern)模式UI
- Builder模式與Java語法UI模式Java
- Builder模式,今天你用了嘛UI模式
- 深入瞭解 Builder 模式 - frankelUI模式
- 《設計模式》 - 3. 建立者模式( Builder )設計模式UI
- 設計模式(五)Builder構建者模式設計模式UI
- Java Builder 模式,你搞懂了麼?JavaUI模式
- 設計模式十: 生成器模式(Builder Pattern)設計模式UI
- 重識設計模式-建造者模式(Builder Pattern)設計模式UI
- C#設計模式-建造者模式(Builder Pattern)C#設計模式UI
- 設計模式系列之建造者模式(Builder Pattern)——複雜物件的組裝與建立設計模式UI物件
- javaBean分析JavaBean
- 人人都會設計模式: 07、建造者模式--Builder設計模式UI
- 十、理解JavaBeanJavaBean
- 標準javabeanJavaBean
- 自由擴充套件你的專案——Builder模式套件UI模式
- 一天一個設計模式(三) - 建造者模式(Builder)設計模式UI
- @Builder.Default 及@BuilderUI
- net core 的Generic Host 之Generic Host BuilderUI
- 使用Builder設計模式實現不變性 - DZone JavaUI設計模式Java
- Builder設計模式結合lombok減少過多傳參UI設計模式Lombok