是什么——什么是建造者模式
建造者模式(Builder Pattern),是一种对象创建型设计模式。它将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
《Effective Java》中提到,在设计类的时候,类的构造方法或静态工厂方法有很多参数的时候,使用建造者模式是一个很好的选择。
UML类图:
- Builder类:抽象建造器类
- ConcreteBuilder类:具体建造器类
- Product类:产品类
-
Director类:组装类
Builder模式UML类图
为什么——为什么要用建造者模式
为了将构建复杂对象的过程和它的部件解耦。
怎么做——怎么实现建造者模式
现在有两种建造者模式,一种是有Director类组装产品类的,另一种则是直接有Builder类组装。
产品类:
/**
* Builder模式产品类
*/
public class Product {
private String partA;
private String partB;
private String partC;
//getter setter方法
//.....
@Override
public String toString() {
return "Product{" +super.toString()+
"\npartA='" + partA + '\'' +
", partB='" + partB + '\'' +
", partC='" + partC + '\'' +
'}';
}
}
有Director类
抽象建造器类:
/**
* Builder模式抽象构造器类
*/
public abstract class Builder {
protected abstract void buildPartA(String partA);
protected abstract void buildPartB(String partB);
protected abstract void buildPartC(String partC);
protected abstract Product getResult();
}
组装类:
public class Director {
private Builder builder;
public Director(Builder builder){
this.builder = builder;
}
public void setBuilder(Builder builder) {
this.builder = builder;
}
/**
* Director类组装产品
*/
public Product construct(String partA,String partB,String partC){
builder.buildPartA(partA);
builder.buildPartB(partB);
builder.buildPartC(partC);
return builder.getResult();
}
}
具体建造者类
/**
* 具体建造者类
*/
public class ProductBuilder extends Builder {
private Product product = new Product();
@Override
protected void buildPartA(String partA) {
product.setPartA(partA);
}
@Override
protected void buildPartB(String partB) {
product.setPartB(partB);
}
@Override
protected void buildPartC(String partC) {
product.setPartC(partC);
}
@Override
protected Product getResult() {
return product;
}
}
直接用Builder类组装
抽象建造者类:
public abstract class WithoutDirectorBuilder {
protected abstract WithoutDirectorBuilder setPartA(String partA);
protected abstract WithoutDirectorBuilder setPartB(String partB);
protected abstract WithoutDirectorBuilder setPartC(String partC);
protected abstract Product create();
}
具体建造者类:
/**
* Builder类直接组装
*/
public class WithoutDirectorProductBuilder extends WithoutDirectorBuilder {
private Product product = new Product();
@Override
protected WithoutDirectorBuilder setPartA(String partA) {
product.setPartA(partA);
return this;
}
@Override
protected WithoutDirectorBuilder setPartB(String partB) {
product.setPartB(partB);
return this;
}
@Override
protected WithoutDirectorBuilder setPartC(String partC) {
product.setPartC(partC);
return this;
}
@Override
protected Product create() {
return product;
}
}
实际使用:
public class Client {
public static void main(String[] args) {
//使用Director类的建造者模式
Builder builder = new ProductBuilder();
Director director = new Director(builder);
Product product = director.construct("part A!","part B!","part C!");
System.out.println(product.toString());
//直接使用Builder类组装的建造者模式
WithoutDirectorProductBuilder withoutDirectorProductBuilder
= new WithoutDirectorProductBuilder();
Product product1 = withoutDirectorProductBuilder
.setPartA("PartA")
.setPartB("PartB")
.setPartC("PartC")
.create();
System.out.println(product1.toString());
}
}
网友评论