建造者模式(Builder Pattern)

建造者模式

 

核心组成

  • Builder:抽象建造者,定义多个通用方法和构建方法
  • ConcreteBuilder:具体建造者,可以有多个
  • Director:指挥者,控制整个组合过程,将需求交给建造者,由建造者去创建对象
  • Product:产品角色

 

优点

  • 客户端不必知道产品内部组成的细节,将产品本身与产品的创建过程解耦
  • 每一个具体建造者都相对独立,而与其他的具体建造者无关,更加精细地控制产品的创建过程
  • 增加新的具体建造者无须修改原有类库的代码,符合开闭原则
  • 建造者模式结合链式编程来使用,代码上更加美观

 

缺点

  • 建造者模式所创建的产品一般具有较多的共同点,如果产品差异大则不建议使用

 

代码示例

builder类(接口)

public interface Builder {

    void buildCpu();

    void buildMainboard();

    void buildDisk();

    void buildPower();

    void buildMemory();

    Computer createComputer();

}

 

产品类(Computer)

public class Computer {

    private String cpu;

    private String mainboard;

    private String disk;

    private String power;

    private String memory;

    public String getCpu() {
        return cpu;
    }

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

    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;
    }

    public String getMemory() {
        return memory;
    }

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


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

 

产品实现类

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 createComputer() {
        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 createComputer() {
        return computer;
    }

}

 

导演类

public class Director {

    public Computer craete(Builder builder){

        builder.buildMemory();
        builder.buildCpu();
        builder.buildMainboard();
        builder.buildDisk();
        builder.buildPower();

        return builder.createComputer();

    }

}

 

main函数

public class BuilderMain {

    public static void main(String[] args) {
        Builder builder = new LowComputerBuilder();
        Computer computer = new Director().craete(builder);
        System.out.println(computer.toString());
    }

}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页