1. 定义
将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示。
2. 实现
2.1 Product
class Product {
private String productName;
private String companyName;
private String part1;
private String part2;
private String part3;
private String part4;
public Product() {
}
public Product(String productName, String companyName, String part1, String part2, String part3, String part4) {
this.productName = productName;
this.companyName = companyName;
this.part1 = part1;
this.part2 = part2;
this.part3 = part3;
this.part4 = part4;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getPart1() {
return part1;
}
public void setPart1(String part1) {
this.part1 = part1;
}
public String getPart2() {
return part2;
}
public void setPart2(String part2) {
this.part2 = part2;
}
public String getPart3() {
return part3;
}
public void setPart3(String part3) {
this.part3 = part3;
}
public String getPart4() {
return part4;
}
public void setPart4(String part4) {
this.part4 = part4;
}
}
2.2 ProductBuilder
interface ProductBuilder {
void builderProductName(String productName);
void builderCompanyName(String companyName);
void builderPart1(String part1);
void builderPart2(String part2);
void builderPart3(String part3);
void builderPart4(String part4);
Product build();
}
2.3 DefaultConcreteProductBuilder
class DefaultConcreteProductBuilder implements ProductBuilder {
private String productName;
private String companyName;
private String part1;
private String part2;
private String part3;
private String part4;
@Override
public void builderProductName(String productName) {
this.productName = productName;
}
@Override
public void builderCompanyName(String companyName) {
this.companyName = companyName;
}
@Override
public void builderPart1(String part1) {
this.part1 = part1;
}
@Override
public void builderPart2(String part2) {
this.part2 = part2;
}
@Override
public void builderPart3(String part3) {
this.part3 = part3;
}
@Override
public void builderPart4(String part4) {
this.part4 = part4;
}
@Override
public Product build() {
return new Product(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
}
}
2.4 SpecialConcreteProductBuilder
class SpecialConcreteProductBuilder implements ProductBuilder {
private String productName;
private String companyName;
private String part1;
private String part2;
private String part3;
private String part4;
@Override
public void builderProductName(String productName) {
this.productName = productName;
}
@Override
public void builderCompanyName(String companyName) {
this.companyName = companyName;
}
@Override
public void builderPart1(String part1) {
this.part1 = part1;
}
@Override
public void builderPart2(String part2) {
this.part2 = part2;
}
@Override
public void builderPart3(String part3) {
this.part3 = part3;
}
@Override
public void builderPart4(String part4) {
this.part4 = part4;
}
@Override
public Product build() {
return new Product(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
}
}
2.5 Director
class Director {
private ProductBuilder builder;
public Director(ProductBuilder builder) {
this.builder = builder;
}
public Product makeProduct(String productName, String companyName, String part1, String part2, String part3, String part4) {
builder.builderProductName(productName);
builder.builderCompanyName(companyName);
builder.builderPart1(part1);
builder.builderPart2(part2);
builder.builderPart3(part3);
builder.builderPart4(part4);
Product product = builder.build();
return product;
}
}
2.6 使用
public static void main(String[] args) {
SpecialConcreteProductBuilder specialConcreteProductBuilder = new SpecialConcreteProductBuilder();
Director director = new Director(specialConcreteProductBuilder);
Product product = director.makeProduct("productName1", "com...", "part1", "part2", "part3", "part4");
System.out.println(product);
}
3. 建造者模式与不可变对象配合使用
3.1 Product
/不可变对象,属性定义为final
class Product {
private final String productName;
private final String companyName;
private final String part1;
private final String part2;
private final String part3;
private final String part4;
public Product(String productName, String companyName, String part1, String part2, String part3, String part4) {
this.productName = productName;
this.companyName = companyName;
this.part1 = part1;
this.part2 = part2;
this.part3 = part3;
this.part4 = part4;
}
// 内部类实现构造器
static class Builder {
private String productName;
private String companyName;
private String part1;
private String part2;
private String part3;
private String part4;
public Builder productName(String productName) {
this.productName = productName;
return this;//返回自己使用时可以用链式
}
public Builder companyName(String companyName) {
this.companyName = companyName;
return this;
}
public Builder part1(String part1) {
this.part1 = part1;
return this;
}
public Builder part2(String part2) {
this.part2 = part2;
return this;
}
public Builder part3(String part3) {
this.part3 = part3;
return this;
}
public Builder part4(String part4) {
this.part4 = part4;
return this;
}
public Product build() {
return new Product(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
}
}
}
3.2 使用
public static void main(String[] args) {
Product.Builder builder = new Product.Builder().productName("productName1").companyName("com.").part1("part1").part2("part2");
builder.part3("part3").part4("part4");
Product product = builder.build();
System.out.println(product);
}