将一个复杂的对象分解为多个简单的对象,然后一步一步构建而成。它将变与不变相分离,即产品的组成部分是不变的,但每一部分是可以灵活选择的。与工厂模式相似,工厂模式注重零部件的制造,建造者注重零部件的组合。
1、具体产品
public class Computer { private String cpu; private String gpu; private String motherboard; private String memory; private String disk; private String power; public void setCpu(String cpu) { this.cpu = cpu; } public void setGpu(String gpu) { this.gpu = gpu; } public void setMotherboard(String motherboard) { this.motherboard = motherboard; } public void setMemory(String memory) { this.memory = memory; } public void setDisk(String disk) { this.disk = disk; } public void setPower(String power) { this.power = power; } public void show() { String computer = "装机配置:" + "cpu=" + cpu + ", gpu=" + gpu + ", motherboard=" + motherboard + ", memory=" + memory + ", disk=" + disk + ", power=" + power; System.out.println(computer); } }
2、抽象建造者
public abstract class Builder { protected Computer computer = new Computer(); public abstract void buildCpu(); public abstract void buildGpu(); public abstract void buildMotherboard(); public abstract void buildMemory(); public abstract void buildDisk(); public abstract void buildPower(); public final Computer getResult() { return computer; } }
3、具体建造者
public class AMDComputerBuilder extends Builder { @Override public void buildCpu() { computer.setCpu("AMD R5-3600"); } @Override public void buildGpu() { computer.setGpu("AMD 5700XT"); } @Override public void buildMotherboard() { computer.setMotherboard("微星 B450"); } @Override public void buildMemory() { computer.setMemory("金士顿 8G * 2"); } @Override public void buildDisk() { computer.setDisk("三星 500G"); } @Override public void buildPower() { computer.setPower("海盗船 600W"); } }
public class IntelComputerBuilder extends Builder{ @Override public void buildCpu() { computer.setCpu("英特尔 i5-9400f"); } @Override public void buildGpu() { computer.setGpu("Nvidia GTX2070s"); } @Override public void buildMotherboard() { computer.setMotherboard("技嘉 大雕"); } @Override public void buildMemory() { computer.setMemory("威刚 8G * 2"); } @Override public void buildDisk() { computer.setDisk("西部数据 1T"); } @Override public void buildPower() { computer.setPower("鑫谷金牌 600W"); } }
4、指挥者
public class Installer { private Builder builder; public Installer(Builder builder) { this.builder = builder; } public Computer construct() { builder.buildCpu(); builder.buildGpu(); builder.buildMotherboard(); builder.buildDisk(); builder.buildMemory(); builder.buildPower(); return builder.getResult(); } }
5、测试
public class Test { public static void main(String[] args) { Builder amd = new AMDComputerBuilder(); Installer installer = new Installer(amd); installer.construct().show(); Builder intel = new IntelComputerBuilder(); installer = new Installer(intel); installer.construct().show(); } }
装机配置:cpu=AMD R5-3600, gpu=AMD 5700XT, motherboard=微星 B450, memory=金士顿 8G * 2, disk=三星 500G, power=海盗船 600W
装机配置:cpu=英特尔 i5-9400f, gpu=Nvidia GTX2070s, motherboard=技嘉 大雕, memory=威刚 8G * 2, disk=西部数据 1T, power=鑫谷金牌 600W
两个具体的建造者根据相同的构建方式,组装了两台配置不一样的电脑。