目的
当我们想要创建一个实体的时候,如果该实体中有大量的参数可以根据需求选择是否传入。
传统实现方式
通常我们有两种方式去实现:
- 重叠构造器
重叠构造器的场景很常见,通常我们会按照需求不断重载构造方法,提供不同的参数。
这样的方式当参数多起来的时候,会变得很难阅读,且十分容易发生传入错误的情况,同时还会存在同类型参数只能有一个存在的情况(如只传入作者的话,就无法重载只传入ISBN或只传入分类的情况)。
Demo:
public class Book {
/**
* 书名和价格为必需,其他内容为可选
*/
private String name; //书名
private float price; //价格
private String author; //作者
private String ISBN; //ISBN
private String type; //分类
public Book(String name, float price) {
this.name = name;
this.price = price;
}
public Book(String name, float price, String author) {
this.name = name;
this.price = price;
this.author = author;
}
public Book(String name, float price, String author, String ISBN) {
this.name = name;
this.price = price;
this.author = author;
this.ISBN = ISBN;
}
}
- JavaBean
使用JavaBean的方式,通过Bean中的get和set方式,创建出的实例会很整洁,读起来也比较容易。
public class Book {
/**
* 书名和价格为必需,其他内容为可选
*/
private String name; //书名
private float price; //价格
private String author; //作者
private String ISBN; //ISBN
private String type; //分类
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Book book = new Book();
book.setName("Hello World");
book.setPrice(100.00f);
book.setAuthor("Coder");
book.setISBN("123465789");
book.setType("technology");
这种方式存在一个问题:构造的过程被分配到了几个调用中,在构造过程中JavaBean可能处于不一致的状态。(具体见“Effective Java”第二章第2条)。
构造器模式(Builder模式)
通过构造器模式,既可以保证安全性,又有了良好的可读性,使用时仅需按需调用即可。
package com.example.buildermodel;
/**
* @author: SuHang
* @date: 2021/9/2
* @desc:
*/
public class Book {
/**
* 书名和价格为必需,其他内容为可选
*/
private final String name; //书名
private final float price; //价格
private final String author; //作者
private final String isbn; //isbn
private final String type; //分类
//创建构造器
public static class Builder {
//必须传入的参数,即书名和价格
private final String name; //书名
private final float price; //价格
//可选传入的参数 --- 初始化默认值
private String author = "Author"; //作者
private String isbn = "isbn"; //isbn
private String type = "TYPE"; //分类
//提供创建实例的构造方法,确保传入必传项。
public Builder(String name, float price) {
this.name = name;
this.price = price;
}
//提供可选参数。
public Builder author(String author) {
this.author = author;
return this;
}
public Builder isbn(String isbn) {
this.isbn = isbn;
return this;
}
public Builder type(String type) {
this.type = type;
return this;
}
//最终创建Book的方法
public Book build() {
return new Book(this);
}
}
private Book(Builder builder) {
name = builder.name;
price = builder.price;
author = builder.author;
isbn = builder.isbn;
type = builder.type;
}
}
Book book = new Book.Builder("Hello World", 100.00f)
.author("Coder")
.isbn("123456789")
.type("technology")
.build();
453

被折叠的 条评论
为什么被折叠?



