上一篇说了工厂方法模式和抽象工厂模式,今来聊聊建造者模式。
一、定义
将一个复杂对象分解成多个相对简单的部分,然后根据不同需要分别创建它们,最后构建成该复杂对象。
类似组将一台电脑,有主机、屏幕、鼠标、键盘等,你将这些组件的型号给组装店说好,他就会根据你的需求组装电脑,但是你不会看见具体的组装过程。
建造者模式隐藏了复杂对象的创建过程,它把复杂对象的创建过程加以抽象,通过子类继承或者重载的方式,动态的创建具有复合属性的对象。
二、实例
像上面说的电脑组装实例,就有三个对象,一个是电脑,一个是组装者,一个是客户。
电脑对象:
public class Computer {
private String screen;
private String keyboard;
private String mouse;
private String host;
//隐藏get、set方法
}
builder类:(相当于每个组件的购买者,具体组装过程是先装什么,后装什么,由组装者决定)
抽象builder类
public interface ComputerBuilder {
public void buildScreen(String screenName);
public void buildHost(String hostName);
public void buildKeyboard(String keyboardName);
public void buildMouse(String mouseName);
public Computer buildComputer();
}
实际builder类
public class ComputerActualBuilder implements ComputerBuilder {
Computer computer = new Computer();
@Override
public void buildScreen(String screenName) {
computer.setScreen(screenName);
}
@Override
public void buildHost(String hostName) {
computer.setHost(hostName);
}
@Override
public void buildKeyboard(String keyboardName) {
computer.setKeyboard(keyboardName);
}
@Override
public void buildMouse(String mouseName) {
computer.setMouse(mouseName);
}
@Override
public Computer buildComputer() {
return computer;
}
}
组装者:
public class Assembler {
ComputerBuilder computerBuilder;
public Assembler(ComputerBuilder computerBuilder) {
this.computerBuilder = computerBuilder;
}
public Computer buildComputer(String scrren,String mouse,String keyboard,String host){
computerBuilder.buildScreen(scrren);
computerBuilder.buildMouse(mouse);
computerBuilder.buildKeyboard(keyboard);
computerBuilder.buildHost(host);
return computerBuilder.buildComputer();
}
}
客户端:
ComputerBuilder computerBuilder = new ComputerActualBuilder();
Assembler assembler = new Assembler(computerBuilder);
Computer computer = assembler.buildComputer("三星屏幕", "雷蛇鼠标", "雷蛇键盘", "外星人主机");
客户只需说出具体需求,组装者就会帮忙组装好,客户并不需要知道具体的组装过程是如何的。
类图如下:
上面是有点复杂的版本,其实还有一个简单的版本,类似与StringBuilder的append方法。
就只有builder类还有电脑类:
比如:
public class Computer {
private String screen;
private String keyboard;
private String mouse;
private String host;
public Computer(ComputerBuilder builder) {
this.screen = builder.screen;
this.keyboard = builder.keyboard;
this.mouse = builder.mouse;
this.host = builder.host;
}
//隐藏get、set方法
static class ComputerBuilder{
private String screen;
private String keyboard;
private String mouse;
private String host;
1、每个建造方法都返回一个当前对象,这样就可以持续调用方法
public ComputerBuilder buildScreen(String screen){
this.screen = screen;
return this;
}
public ComputerBuilder buildKeyboard(String keyboard){
this.keyboard = keyboard;
return this;
}
public ComputerBuilder buildMouse(String mouse){
this.mouse = mouse;
return this;
}
public ComputerBuilder buildHost(String host){
this.host = host;
return this;
}
2、最后将当前对象作为参数构造Computer
public Computer build(){
return new Computer(this);
}
}
}
上面的使用方法如下:
Computer computer = new Computer.ComputerBuilder()
.buildMouse("雷蛇鼠标")
.buildHost("外星人主机")
.buildKeyboard("雷蛇键盘")
.buildScreen("三星屏幕").build();
System.out.println(computer);
交由静态内部类ComputerBuilder去实现建造过程。
三、源码实例
(1)StringBuffer和SpringBuilder的append方法就是建造者的
(2)代码中用TransportClient连接es的时候,需要设置连接属性的Settings,
ok,这个就是建造者模式!
希望能帮到你。