创建者模式(Builder Pattern)

建造者模式(builder pattern)

定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

个人理解

  1. 建造者模式就是将复杂的对象创建细节封装起来,客户端在使用的时候更加简洁明了。提高可读性。

  2. 对于步骤相同但是内容不同情况,创建一个builder就可以完成简单的扩展.

参与者

  • 抽象创建者(Builder)
  • 具体建造者(ConcreteBuilder)
  • 指挥者(Director)
  • 具体产品(Product)
代码案例

产品类 product

public class Product {
protected List<String> parts = new ArrayList<String>();

public void add(String part){
    parts.add(part);
}

public void show(){
    System.out.println("产品部件信息:");
    for (String part : parts) {
        System.out.println(part + "\t");
    }
}

builder接口/抽象类

public interface Builder {
    void buildCPU();
    void  buildMemory();
    void buildDisplayCart();
    Product getFinalResult();
}

productBuilder产品的创建者

public class AcerBuilder implements Builder{
    private Product product = new Product();

    @Override
    public void buildCPU() {
     product.add("CPU:Intel 酷睿i3");
    }

    @Override
    public void buildMemory() {
        product.add("内存:4GB DDR3");
    }

    @Override
    public void buildDisplayCart() {
        product.add("显卡:NAVDIA ");
    }

    @Override
    public Product getFinalResult() {
        return product;
    }
}

direcotr构建者

public class Director {

    private Builder builder;

    public Director(Builder builder){
        this.builder = builder;
    }

    public void construct(){
        builder.buildCPU();
        builder.buildDisplayCart();
        builder.buildMemory();
    }
}
UML

具体产品。

创建者模式(Builder Pattern)_第1张图片

抽象产品
创建者模式(Builder Pattern)_第2张图片

链式写法

当一个类的构造函数参数超过4个,而且这些参数有些是可选的时,可以使用建造者模式

public class ChainBuilder {
    private String cpu;//必须
    private String ram;//必须
    private int usbCount;//可选
    private String keyboard;//可选
    private String display;//可选

    public static class  Builder{
        private String cpu;//必须
        private String ram;//必须
        private int usbCount =2;//可选
        private String keyboard="罗技";//可选
        private String display="华硕";//可选
        public Builder(String cpu,String ram){
            this.cpu = cpu;
            this.ram = ram;
        }
        public Builder usbCount(int num){
            this.usbCount = num;
            return this;
        }
        public Builder keyboard(String keyboard){
            this.keyboard = keyboard;
            return this;
        }
        public Builder display(String display){
            this.display = display;
            return this;
        }

        private ChainBuilder build( ){
            return new ChainBuilder(this);
        }
    }
    public ChainBuilder(Builder builder){
        this.cpu=builder.cpu;
        this.ram=builder.ram;
        this.usbCount=builder.usbCount;
        this.display=builder.display;
        this.keyboard=builder.keyboard;
    }

    public static void main(String[] args) {
        ChainBuilder build = new ChainBuilder.Builder("Intel", "ddr3").usbCount(12).build();
        System.out.println(build.toString());
    }

    @Override
    public String toString() {
        return "ChainBuilder{" +
                "cpu='" + cpu + '\'' +
                ", ram='" + ram + '\'' +
                ", usbCount=" + usbCount +
                ", keyboard='" + keyboard + '\'' +
                ", display='" + display + '\'' +
                '}';
    }
}

你可能感兴趣的:(设计模式,建造者模式)