核心思想

  • 抽取共同的行为,允许使用者指定复杂对象的类型和内容,不需要了解内部的构建细节
  • 使用多个简单的行为构建一个复杂的对象,将对象的构建过程和它的表示分离,同样的构建过程可以创建不同的表示

设计模式-建造者模式-LMLPHP

优缺点

优点

  • 使用者不需要知道具体内部组成细节,产品本身与创建过程解耦
  • 具体建造者每个都是独立的,相互无关,不同的是控制产品的创建过程
  • 新增具体建造者不需要修改原来类的代码

缺点

  • 建造者所创建的产品一般要具有比较多的共同点,差异大的话不建议使用

代码示例

指挥者代码


/**
 * 指挥者
 */
public class Director {

    /**
     * 构建对象
     * @param builder
     * @return
     */
    public Computer createComputer(Builder builder){
        builder.buildMemory();
        builder.buildCpu();
        builder.buildMainboard();
        builder.buildDisk();
        builder.buildPower();
        return builder.getComputer();
    }
}

抽象建造者

/**
 * 建造者的公共方法
 */
public interface Builder {

    /**
     *方法
     */
    void buildCpu();

    void buildMainboard();

    void buildDisk();

    void buildPower();

    void buildMemory();

    Computer getComputer();
}

具体建造者


public class HighComputerBuilder implements Builder{

    private Computer computer = new Computer();

    @Override
    public void buildCpu() {
        computer.setCpu("高配 CPU");
    }

    @Override
    public void buildMainboard() {
        computer.setMainboard("高配 主板");
    }

    @Override
    public void buildDisk() {
        computer.setDisk("高配 磁盘");
    }

    @Override
    public void buildPower() {
        computer.setPower("高配 电源");
    }

    @Override
    public void buildMemory() {
        computer.setMemory("高配 内存");
    }

    @Override
    public Computer getComputer() {
        return computer;
    }
}
public class LowComputerBuilder implements Builder{

    private Computer computer = new Computer();

    @Override
    public void buildCpu() {
        computer.setCpu("低配 CPU");
    }

    @Override
    public void buildMainboard() {
        computer.setMainboard("低配 主板");
    }

    @Override
    public void buildDisk() {
        computer.setDisk("低配 磁盘");
    }

    @Override
    public void buildPower() {
        computer.setPower("低配 电源");
    }

    @Override
    public void buildMemory() {
        computer.setMemory("低配 内存");
    }

    @Override
    public Computer getComputer() {
        return computer;
    }
}

产品


public class Computer {

    private String cpu;

    private String memory;

    private String mainboard;

    private String disk;

    private String power;

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getMemory() {
        return memory;
    }

    public void setMemory(String memory) {
        this.memory = memory;
    }

    public String getMainboard() {
        return mainboard;
    }

    public void setMainboard(String mainboard) {
        this.mainboard = mainboard;
    }

    public String getDisk() {
        return disk;
    }

    public void setDisk(String disk) {
        this.disk = disk;
    }

    public String getPower() {
        return power;
    }

    public void setPower(String power) {
        this.power = power;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu='" + cpu + '\'' +
                ", memory='" + memory + '\'' +
                ", mainboard='" + mainboard + '\'' +
                ", disk='" + disk + '\'' +
                ", power='" + power + '\'' +
                '}';
    }
}

测试

public class AppRun {
    public static void main(String[] args) {

        Director director = new Director();
        Computer computer = director.createComputer(new HighComputerBuilder());
        System.out.println(computer);

    }
}

设计模式-建造者模式-LMLPHP

public class AppRun {
    public static void main(String[] args) {

        Director director = new Director();
        Computer computer = director.createComputer(new LowComputerBuilder());
        System.out.println(computer);

    }
}

设计模式-建造者模式-LMLPHP

08-23 08:13