参考标明出处
工厂类模式提供的是创建单个类的模式,也就是它注重创建实例的结果,而建造者模式则是将各种产品集中起来进行管理,用来创建复合对象,所谓复合对象就是指某个类具有不同的属性,其实建造者模式就是抽象工厂模式和最后的客户端使用结合起来得到的。
建造者模式的四个角色
示例
public abstract class Builder {
//打地基
public abstract void buildBasic();
//砌墙
public abstract void buildWalls();
//封顶
public abstract void roofed();
//完工,获取产品
public abstract House build();
}
具体建造者
public class ConcreteBuilder extends Builder{
private House house;
public ConcreteBuilder(){
this.house = new House();
}
@Override
public void buildBasic() {
house.setBasic("打地基");
}
@Override
public void buildWalls() {
house.setWall("砌墙");
}
@Override
public void roofed() {
house.setRoofed("封顶");
}
@Override
public House build() {
return this.house;
}
}
需要获取的实例对象,即产品
public class House {
private String basic;
private String wall;
private String roofed;
public String getBasic() {
return basic;
}
public void setBasic(String basic) {
this.basic = basic;
}
public String getWall() {
return wall;
}
public void setWall(String wall) {
this.wall = wall;
}
public String getRoofed() {
return roofed;
}
public void setRoofed(String roofed) {
this.roofed = roofed;
}
@Override
public String toString() {
return "House{" +
"basic='" + basic + '\'' +
", wall='" + wall + '\'' +
", roofed='" + roofed + '\'' +
'}';
}
}
指挥者
指定需要获取的实例创建的过程以及其它的一些信息;
public class Director {
//指定顺序建造
public House create(Builder builder) {
builder.buildBasic();
builder.buildWalls();
builder.roofed();
return builder.build();
}
}
客户端的获取
public class TestBuilder {
public static void main(String[] args) {
Director director = new Director();
ConcreteBuilder concreteBuilder = new ConcreteBuilder();
House house = director.create(concreteBuilder);
System.out.println(house);
}
}
结果如下,获取到了该实例,并且给属性都赋值了;如果我们要建造不同的“房子”那么可以创建不同的 House 的子类;
House{basic='打地基', wall='砌墙', roofed='封顶'}
当一个类构造器需要传入很多参数时,如果创建这个类的实例,代码可读性会非常差,而且很容易引入错误,此时就可以利用静态内部类进行建造者模式的重构。
//将产品与抽象建造者类还有指挥者融合在一起
public class ComputerBuilder {
private String cpu;
private String screen;
private String memory;
private String mainboard;
//省略setter和getter方法
@Override
public String toString() {
return "ComputerBuilder{" +
"cpu='" + cpu + '\'' +
", screen='" + screen + '\'' +
", memory='" + memory + '\'' +
", mainboard='" + mainboard + '\'' +
'}';
}
public ComputerBuilder() {
}
//将静态内部类的实例传入并且为真实创建的实例属性赋值
private ComputerBuilder(Builder builder) {
cpu = builder.cpu;
screen = builder.screen;
memory = builder.memory;
mainboard = builder.mainboard;
}
public static final class Builder {
private String cpu;
private String screen;
private String memory;
private String mainboard;
public Builder() {
}
public Builder cpu(String val) {
cpu = val;
return this;
}
public Builder screen(String val) {
screen = val;
return this;
}
public Builder memory(String val) {
memory = val;
return this;
}
public Builder mainboard(String val) {
mainboard = val;
return this;
}
public ComputerBuilder build() {
return new ComputerBuilder(this);
}
}
}
客户端调用
public class Client {
public static void main(String[] args) {
//Computer computer = new Computer("i5", "戴尔", "金士顿", "其它");
ComputerBuilder build = new ComputerBuilder.Builder()
.cpu("i5").
screen("dell").
memory("金士顿").
mainboard("其它").build();
System.out.println(build);
}
}
上面的示例代码只是传入四个参数,如果参数是十四个甚至更多,builder 模式的优势将会更加明显,传递参数更加灵活,代码具有更高的可读性。