将一个复杂的对象的结构与他的表示分离,使得同样的构建过程可以创建不同的表示,比如电脑是一个对象,有显示器、处理器、操作系统等,一个建造类可以建造出不同的电脑;
Android中的AlertDialog
角色:
- Builder:给出一个抽象接口,以规范产品对象的各个组成成分的建造。这个接口规定要实现复杂对象的哪些部分的创建,并不涉及具体的对象部件的创建。
- ConcreteBuilder:实现Builder接口,针对不同的商业逻辑,具体化复杂对象的各部分的创建。 在建造过程完成后,提供产品的实例。
- Director:调用具体建造者来创建复杂对象的各个部分,在指导者中不涉及具体产品的信息,只负责保证对象各部分完整创建或按某种顺序创建。
- Product:要创建的复杂对象。
例子:
电脑组装,产品结果都是电脑,但是配置不同,最终拿给用户的不同电脑,在于组装者(建造者)。
- 电脑是产品:Computer
- 抽象的建造者:ComputerBuilder
- 开发电脑的具体建造者:DevComputerBuilder
- 商务电脑的具体建造者:BusinessComputerBuilder
- 组装电脑的内容流程:Director
- 客户端:
Computer
/**
* Created by jarrysong on 2017/7/28.
* Product
*/
public class Computer {
public String screen;
public String cpu;
public String memory;
public String hardDisk;
@Override
public String toString() {
return "Computer{" +
"screen='" + screen + '\'' +
", cpu='" + cpu + '\'' +
", memory='" + memory + '\'' +
", hardDisk='" + hardDisk + '\'' +
'}';
}
}
ComputerBuilder
/**
* Created by jarrysong on 2017/7/28.
* 构建电脑Builder
*/
public abstract class ComputerBuilder {
public abstract void buildScreen();
public abstract void buildCPU();
public abstract void buildMemory();
public abstract void buildHardDisk();
public abstract Computer getComputer();
}
DevComputerBuilder
/**
* Created by jarrysong on 2017/7/28.
* ConcreteBuilder
*/
public class DevComputerBuilder extends ComputerBuilder{
private Computer devComputer=new Computer();
@Override
public void buildScreen() {
devComputer.screen="30寸";
}
@Override
public void buildCPU() {
devComputer.cpu="i7";
}
@Override
public void buildMemory() {
devComputer.memory="16G";
}
@Override
public void buildHardDisk() {
devComputer.hardDisk="dds 128G";
}
@Override
public Computer getComputer() {
return devComputer;
}
}
BusinessComputerBuilder
/**
* Created by jarrysong on 2017/7/28.
*/
public class BusinessComputerBuilder extends ComputerBuilder{
Computer businessComputer=new Computer();
@Override
public void buildScreen() {
businessComputer.screen="20寸";
}
@Override
public void buildCPU() {
businessComputer.cpu="i5";
}
@Override
public void buildMemory() {
businessComputer.memory="4G";
}
@Override
public void buildHardDisk() {
businessComputer.hardDisk="500G";
}
@Override
public Computer getComputer() {
return businessComputer;
}
}
Director
/**
* Created by jarrysong on 2017/7/28.
* Director
*/
public class Director {
public void createComputer(ComputerBuilder builder){
builder.buildCPU();
builder.buildHardDisk();
builder.buildMemory();
builder.buildScreen();
}
}
Client
/**
* Created by jarrysong on 2017/7/28.
*/
public class Client {
@Test
public void createComputer(){
DevComputerBuilder devComputerBuilder=new DevComputerBuilder();
BusinessComputerBuilder businessComputerBuild=new BusinessComputerBuilder();
Director director=new Director();
director.createComputer(devComputerBuilder);
director.createComputer(businessComputerBuild);
Computer debComputer=devComputerBuilder.getComputer();
Computer businessComputer=businessComputerBuild.getComputer();
System.out.println(debComputer.toString());
System.out.println(businessComputer.toString());
}
}