是什么?
建造者模式是一步一步创建一个复杂的对象,形如Builder.builder.a().b().c().build();它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。
构建者模式提供了一种极佳的复杂对象的创建方式。
一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。
适用场景?
复杂对象的创建场景
一个复杂对象由多个零部件构成,用户只需一步一步安装零部件即可
代码实现
简单对象演示
假设一个产品对象Product只有一个属性name产品名称,其构建者模式如下:
/**
* 简单对象-产品类
* 只有一个属性:产品名称
*
* 演示构建者模式
*/
public class Product {
//产品名称
private String name;
//全参构造函数
public Product(String name) {
this.name = name;
}
//获取构建者
public static ProductBuilder builder(){
return new ProductBuilder();
}
//产品构造器提供者
static class ProductBuilder{
//复制Product的所有属性,作为暂存备份,构建对象时线程安全
//产品名称
private String name;
public ProductBuilder name(String name){
this.name=name;
return this;
}
//构建对象方法
public Product build(){
//一次性new对象,保证线程安全
return new Product(name);
}
}
}
使用非常简单方便
Product.builder().name("产品名称").build();
复杂对象演示
复杂对象才能体现构建者模式的价值,
假设一个复杂的产品,由多个部件组成,使用构建者模式创建该产品的过程如下:
//定义产品
/**
* 复杂产品由多个部分组成
* PartA A部分
* PartB B部分
* 等等
*/
public class Product {
private Integer id;//产品唯一标识
private String name;//产品名称
private PartA partA;//零部件A
private PartB partB;//零部件B
static class PartA {
private String name;//零部件A的名称
}
static class PartB {
private Integer id;//零部件B的唯一标识
private String name;//零部件B的名称
private PartA partA;//零部件A
}
}
构造者模式创建如上复杂产品对象,为产品Product创建构建者模式类
/构造类对象
public static class ProductBuilder {
//冗余对象,保证创建对象得线程安全
private Integer id;
private String name;
private PartA partA;
private PartB partB;
ProductBuilder() {
}
public ProductBuilder id(final Integer id) {
this.id = id;
return this;
}
public ProductBuilder name(final String name) {
this.name = name;
return this;
}
public ProductBuilder partA(final PartA partA) {
this.partA = partA;
return this;
}
public ProductBuilder partB(final PartB partB) {
this.partB = partB;
return this;
}
public Product build() {
return new Product(this.id, this.name, this.partA, this.partB);
}
}
同理给PartA和PartB创建构造者模式类
public static class PartABuilder {
private String name;
PartABuilder() {
}
public PartA.PartABuilder name(final String name) {
this.name = name;
return this;
}
public PartA build() {
return new PartA(this.name);
}
}
public static class PartBBuilder {
private Integer id;
private String name;
private PartA partA;
PartBBuilder() {
}
public PartB.PartBBuilder id(final Integer id) {
this.id = id;
return this;
}
public PartB.PartBBuilder name(final String name) {
this.name = name;
return this;
}
public PartB.PartBBuilder partA(final PartA partA) {
this.partA = partA;
return this;
}
public PartB build() {
return new PartB(this.id, this.name, this.partA);
}
}
类全貌如下:
/**
* 复杂产品由多个部分组成
* PartA A部分
* PartB B部分
* 等等
*/
public class Product {
private Integer id;//产品唯一标识
private String name;//产品名称
private PartA partA;//产品A部件
private PartB partB;//产品B部件
//全参构造方法
Product(final Integer id, final String name, final PartA partA, final PartB partB) {
this.id = id;
this.name = name;
this.partA = partA;
this.partB = partB;
}
//构造方法
public static ProductBuilder builder() {
return new ProductBuilder();
}
//构造类对象
public static class ProductBuilder {
//冗余对象,保证创建对象得线程安全
private Integer id;
private String name;
private PartA partA;
private PartB partB;
ProductBuilder() {
}
public ProductBuilder id(final Integer id) {
this.id = id;
return this;
}
public ProductBuilder name(final String name) {
this.name = name;
return this;
}
public ProductBuilder partA(final PartA partA) {
this.partA = partA;
return this;
}
public ProductBuilder partB(final PartB partB) {
this.partB = partB;
return this;
}
public Product build() {
return new Product(this.id, this.name, this.partA, this.partB);
}
}
private static class PartA {
private String name;
PartA(final String name) {
this.name = name;
}
public static PartA.PartABuilder builder() {
return new PartA.PartABuilder();
}
public static class PartABuilder {
private String name;
PartABuilder() {
}
public PartA.PartABuilder name(final String name) {
this.name = name;
return this;
}
public PartA build() {
return new PartA(this.name);
}
}
}
private static class PartB {
private Integer id;
private String name;
private PartA partA;
PartB(final Integer id, final String name, final PartA partA) {
this.id = id;
this.name = name;
this.partA = partA;
}
public static PartB.PartBBuilder builder() {
return new PartB.PartBBuilder();
}
public static class PartBBuilder {
private Integer id;
private String name;
private PartA partA;
PartBBuilder() {
}
public PartB.PartBBuilder id(final Integer id) {
this.id = id;
return this;
}
public PartB.PartBBuilder name(final String name) {
this.name = name;
return this;
}
public PartB.PartBBuilder partA(final PartA partA) {
this.partA = partA;
return this;
}
public PartB build() {
return new PartB(this.id, this.name, this.partA);
}
}
}
}
测试代码,构建者模式创建对象:
Product build = Product.builder()
.id(1)
.name("测试产品")
.partA(
PartA
.builder()
.name("零部件A")
.build()
)
.partB(
PartB
.builder()
.id(1)
.name("测试产品")
.partA(
PartA
.builder()
.name("零部件A")
.build()
)
.build()
)
.build();