什么时候使用:当一类复杂对象的一些内部组件形式是不变的,而真正的组件却各不相同时,创建这类对象可以用建造者模式。
与工厂模式的区别:
-
工厂模式,只创建出来一个产品,不关心这个产品的内部细节,构造时通常传产品的参数,产生一个封装的对象,栗子.我要一个联想的ThinkPad;
-
建造者模式,也创建一个产品,但是还能制定好这个产品的内部细节,只要Director有这个工艺流程,构造时通常传产品内部的参数,产生一个灵活的对象。栗子.我要一个cpu是amd的,显卡是英伟达的,显示器是三星的电脑;
一种复杂的实现:
public class BuilderTest2 {
public static void main(String[] args) {
//new一个王师傅,干泥瓦匠的
ProductBuilder productBuilder = new DefaultConcreteProductBuilder();
//new一个包工头,把王师傅给他
Director director = new Director(productBuilder);
//面向包工头得到建好的房子
Product product = director.getProduct("xxx", "xx", "x1", "x2", "x3");
System.out.println(product);
}
}
//(泥瓦匠工种)
interface ProductBuilder{
void buildProductName(String productName);
void buildCompanyName(String companyName);
void buildPart1(String part1);
void buildPart2(String part2);
void buildPart3(String part3);
Product build();
}
//可以有其他的建造者,来实现ProductBuilder接口(泥瓦匠王师傅)
class DefaultConcreteProductBuilder implements ProductBuilder{
private String productName;
private String companyName;
private String part1;
private String part2;
private String part3;
public void buildProductName(String productName) {
this.productName = productName;
}
public void buildCompanyName(String companyName) {
this.companyName = companyName;
}
public void buildPart1(String part1) {
this.part1 = part1;
}
public void buildPart2(String part2) {
this.part2 = part2;
}
public void buildPart3(String part3) {
this.part3 = part3;
}
public Product build() {
return new Product(this.productName,this.companyName,this.part1,this.part2,this.part3);
}
}
//指导如何建造(工地领导)
class Director{
//注入建造者,只要它实现了ProductBuilder接口
private ProductBuilder builder;
public Director(ProductBuilder builder) {
this.builder = builder;
}
//建造对象
public Product getProduct(String productName, String companyName, String part1, String part2, String part3){
builder.buildProductName(productName);
builder.buildCompanyName(companyName);
builder.buildPart1(part1);
builder.buildPart2(part2);
builder.buildPart3(part3);
Product product = builder.build();
return product;
}
}
//要创建的对象(工地建筑)
class Product{
private String productName;
private String companyName;
private String part1;
private String part2;
private String part3;
public Product(String productName, String companyName, String part1, String part2, String part3) {
this.productName = productName;
this.companyName = companyName;
this.part1 = part1;
this.part2 = part2;
this.part3 = part3;
}
@Override
public String toString() {
return "Product{" +
"productName='" + productName + '\'' +
", companyName='" + companyName + '\'' +
", part1='" + part1 + '\'' +
", part2='" + part2 + '\'' +
", part3='" + part3 + '\'' +
'}';
}
}
一种简单的实现:
public class BuilderTest {
public static void main(String[] args) {
Product product = new Product.Builder().productName("xxx").companyName("xx").part1("x").build();
System.out.println(product);
}
}
//产品类
class Product{
//除了直接建造产品本身,还可以建造不同产品的顺序list。此时要定义一个接口,不同的item实现这个接口,再用一个list保存这些item
//这是一种赋值后固定不变的产品,当然也可以不指定为final
private final String productName;
private final String companyName;
private final String part1;
private final String part2;
private final String part3;
public Product(String productName, String companyName, String part1, String part2, String part3) {
this.productName = productName;
this.companyName = companyName;
this.part1 = part1;
this.part2 = part2;
this.part3 = part3;
}
@Override
public String toString() {
return "Product{" +
"productName='" + productName + '\'' +
", companyName='" + companyName + '\'' +
", part1='" + part1 + '\'' +
", part2='" + part2 + '\'' +
", part3='" + part3 + '\'' +
'}';
}
//产品内部静态类,用于建造产品
static class Builder{
//与产品属性保持一致
private String productName;
private String companyName;
private String part1;
private String part2;
private String part3;
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;
}
//建造产品方法
Product build(){
//这里可以加校验产品代码
Product product = new Product(this.productName, this.companyName, this.part1, this.part2, this.part3);
return product;
}
}
}
应用场景
StringBuilder;
Accessors注解