设计模式-建造者模式

2019-12-13  本文已影响0人  蜗牛写java

设计模式-建造者模式

概念

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

用户只需指定需要建造的类型就可以得到他们,建造过程及细节不需要知道

类型:创建型

使用场景

优点

缺点

UML

建造者模式UML.png

实例

我们以组装电脑为例:组装电脑需要主板、CPU、内存、硬盘等等

UML

实例UML.png

产品类

public class Computer {

    //各个组件 与 电脑 是整体与部分关系;该场景主要是建造者模式;组件就不在抽象,均以Object为类型
    private Object cpu;
    private Object mainBoard;
    private Object memory;
    private Object disk;
    private Object power;

    public Object getCpu() {
        return cpu;
    }
    public void setCpu(Object cpu) {
        this.cpu = cpu;
    }

    public Object getMainBoard() {
        return mainBoard;
    }
    public void setMainBoard(Object mainBoard) {
        this.mainBoard = mainBoard;
    }

    public Object getMemory() {
        return memory;
    }
    public void setMemory(Object memory) {
        this.memory = memory;
    }

    public Object getDisk() {
        return disk;
    }
    public void setDisk(Object disk) {
        this.disk = disk;
    }

    public Object getPower() {
        return power;
    }
    public void setPower(Object power) {
        this.power = power;
    }

    @Override
    public String toString() {
        return "{" +
                "cpu:" + cpu + ",  " +
                "mainBoard:" + mainBoard + ",  " +
                "memory:" + memory + ",  " +
                "disk:" + disk + ",  " +
                "power:" + power +
                "}";
    }
}

抽象建造者

public interface IComputerBuilder {

    void buildCpu(Object cpu);

    void buildMainBoard(Object mainBoard);

    void buildMemory(Object memory);

    void buildDisk(Object disk);

    void buildPower(Object power);

    Computer getComputer();
}

具体构建者

public class ComputerBuilderImpl implements IComputerBuilder {

    private Computer computer = new Computer();

    @Override
    public void buildCpu(Object cpu) {
        computer.setCpu(cpu);
    }

    @Override
    public void buildMainBoard(Object mainBoard) {
        computer.setMainBoard(mainBoard);
    }

    @Override
    public void buildMemory(Object memory) {
        computer.setMemory(memory);
    }

    @Override
    public void buildDisk(Object disk) {
        computer.setDisk(disk);
    }

    @Override
    public void buildPower(Object power) {
        computer.setPower(power);
    }

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

导演类

public class Director {

    public void construct(IComputerBuilder builder) {
        builder.buildCpu("i5");
        builder.buildDisk("500G");
        builder.buildMainBoard("hp-MainBoard");
        builder.buildMemory("16G");
        builder.buildPower("hp-Power");
    }
}

使用场景

public class Client {
    // 一个消费者向电脑门店购买电脑;电脑商家组件电脑后,卖给消费者
    public static void main(String[] args) {
        IComputerBuilder builder = new ComputerBuilderImpl();
        Director director = new Director();
        director.construct(builder);

        Computer computer = builder.getComputer();

        System.out.println(computer);
    }
}

源码案例

  1. StringBuilder (源码做了提取)

    abstract class AbstractStringBuilder implements Appendable, CharSequence {
        char[] value;
        int count;
        
        public AbstractStringBuilder append(String str) {
            if (str == null)
                return appendNull();
            int len = str.length();
            ensureCapacityInternal(count + len);
            //将 value 插入到 str 对应的char数组中;从0开始插入
            str.getChars(0, len, value, count);
            
            count += len;
            //返回自己
            return this;
        }
    }
    
public final class StringBuilder extends AbstractStringBuilder 
implements java.io.Serializable, CharSequence {

 //组织产品零件
 @Override
    public StringBuilder append(Object obj) {
        return append(String.valueOf(obj));
    }

    //组织产品零件
    @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }
    
    //构建出产品
    @Override
    public String toString() {
        // Create a copy, don't share the array
        return new String(value, 0, count);
    }

}
  1. ES-BulkProcessor (静态内部类方式)

    public class BulkProcessor implements Closeable {
        //...无关代码
        //构造器,该构造器 不是 public,由静态内部类构建
        BulkProcessor(Client client, 
                      BackoffPolicy backoffPolicy, 
                      Listener listener, 
                      @Nullable String name, 
                      int concurrentRequests, 
                      int bulkActions, ByteSizeValue bulkSize, 
                      @Nullable TimeValue flushInterval) {
            //属性设置
            this.... = ...
        }
        
        //静态内部类构建 对象
        public static class Builder {
            private final Client client;
            private final Listener listener;
    
            private String name;
            private int concurrentRequests = 1;
            private int bulkActions = 1000;
            private ByteSizeValue bulkSize = new ByteSizeValue(5, ByteSizeUnit.MB);
            private TimeValue flushInterval = null;
            private BackoffPolicy backoffPolicy = BackoffPolicy.exponentialBackoff();
    
            public Builder(Client client, Listener listener) {
                this.client = client;
                this.listener = listener;
            }
            
            public Builder setName(String name) {
                this.name = name;
                return this;
            }
            
            public Builder setConcurrentRequests(int concurrentRequests) {
                this.concurrentRequests = concurrentRequests;
                return this;
            }
            
            public Builder setBulkActions(int bulkActions) {
                this.bulkActions = bulkActions;
                return this;
            }
    
            public Builder setBulkSize(ByteSizeValue bulkSize) {
                this.bulkSize = bulkSize;
                return this;
            }
    
            public Builder setFlushInterval(TimeValue flushInterval) {
                this.flushInterval = flushInterval;
                return this;
            }
    
            public Builder setBackoffPolicy(BackoffPolicy backoffPolicy) {
                this.backoffPolicy = backoffPolicy;
                return this;
            }
    
            public BulkProcessor build() {
                return new BulkProcessor(client, backoffPolicy, listener, name, concurrentRequests, bulkActions, bulkSize, flushInterval);
            }
        }
    }
    
上一篇下一篇

猜你喜欢

热点阅读