avatar

建造者模式
  1. 建造者模式(Builder Pattern) 也叫生成器模式,其定义如下:
    Separate the construction of a complex object from its representation so that the same construction process can create different representations.(将一个复杂对象的构建与他的表示分离,是得同样的构建过程可以创建不同的表示。)
    在建造者模式中,有如下4个角色:
  • Product产品类
    通常是实现了模板方法模式,也就是有模板方法和基本方法。
  • Builder抽象建造者
    规范产品的组件,一般是由子类实现。例子中的CarBuilder就属于抽象建造者。
  • ConcreteBuilder具体建造者
    实现抽象类定义的所有方法,并且返回一个组建好的对象。
  • Director导演类
    负责安排已有模块的顺序,然后告诉Builder开始建造,在上面的例子中就是我们的老大,xx公司找到老大,说我要这个或那个类型的车辆模型,然后老大就把命令传递给我,我和我的团队就开始拼命的建造,于是一个项目建设完毕了。
    建造者模式的通用源码也比较简单,先看Product类,通常它是一个组合或继承(如模板方法模式)产生的类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    package com.wzl.fea;

    import java.util.ArrayList;

    abstract class CarBuilder {
    public abstract void setSequence(ArrayList<String> sequence);
    public abstract CarModel getCarModel();
    }

    abstract class CarModel {
    private ArrayList<String> sequence = new ArrayList<String>();
    protected abstract void start();
    protected abstract void stop();
    protected abstract void alarm();
    protected abstract void engineBoom();

    final public void run() {
    for (int i = 0; i < this.sequence.size(); ++i) {
    String actionName = this.sequence.get(i);
    if (actionName.equalsIgnoreCase("start")) {
    this.start();
    }
    else if (actionName.equalsIgnoreCase("stop")) {
    this.stop();
    }
    else if (actionName.equalsIgnoreCase("alarm")) {
    this.alarm();
    }
    else if (actionName.equalsIgnoreCase("engine boom")) {
    //如果是engine boom关键字
    this.engineBoom();
    }
    }
    }

    final public void setSequence(ArrayList sequence){
    this.sequence = sequence;
    }
    }

    class BenzModel extends CarModel {
    protected void alarm() {
    System.out.println("奔驰车的喇叭声音是这个样子的...");
    }
    protected void engineBoom() {
    System.out.println("奔驰车的引擎是这个声音的...");
    }
    protected void start() {
    System.out.println("奔驰车跑起来是这个样子的...");
    }
    protected void stop() {
    System.out.println("奔驰车应该这样停车...");
    }
    }

    class BMWModel extends CarModel {
    protected void alarm() {
    System.out.println("宝马车的喇叭声音是这个样子的...");
    }
    protected void engineBoom() {
    System.out.println("宝马车的引擎是这个声音的...");
    }
    protected void start() {
    System.out.println("宝马车跑起来是这个样子的...");
    }
    protected void stop() {
    System.out.println("宝马车应该这样停车...");
    }
    }

    class BenzBuilder extends CarBuilder {
    private BenzModel benz = new BenzModel();

    @Override
    public CarModel getCarModel() {
    return this.benz;
    }

    @Override
    public void setSequence(ArrayList<String> sequence) {
    this.benz.setSequence(sequence);
    }
    }

    class BMWBuilder extends CarBuilder {
    private BMWModel bmw = new BMWModel();

    @Override
    public CarModel getCarModel() {
    return this.bmw;
    }

    @Override
    public void setSequence(ArrayList<String> sequence) {
    this.bmw.setSequence(sequence);
    }
    }

    class CarDirector {
    private ArrayList<String> sequence = new ArrayList<>();
    private BenzBuilder benzBuilder = new BenzBuilder();
    private BMWBuilder bmwBuilder = new BMWBuilder();

    public BenzModel getABenzModel(){
    this.sequence.clear();
    this.sequence.add("start");
    this.sequence.add("stop");
    this.benzBuilder.setSequence(this.sequence);
    return (BenzModel)this.benzBuilder.getCarModel();
    }

    public BenzModel getBBenzModel(){
    this.sequence.clear();
    this.sequence.add("engine boom");
    this.sequence.add("start");
    this.sequence.add("stop");
    this.benzBuilder.setSequence(this.sequence);
    return (BenzModel)this.benzBuilder.getCarModel();
    }

    public BMWModel getCBMWModel(){
    this.sequence.clear();
    this.sequence.add("alarm");
    this.sequence.add("start");
    this.sequence.add("stop");
    this.bmwBuilder.setSequence(this.sequence);
    return (BMWModel)this.bmwBuilder.getCarModel();
    }

    public BMWModel getDBMWModel(){
    this.sequence.clear();
    this.sequence.add("start");
    this.bmwBuilder.setSequence(this.sequence);
    return (BMWModel)this.benzBuilder.getCarModel();
    }
    }


    public class EpCarBuilder {
    public static void main(String[] args) {
    ArrayList<String> sequence = new ArrayList<String>();
    sequence.add("engine boom");
    sequence.add("start");
    sequence.add("stop");

    BenzBuilder benzBuilder = new BenzBuilder();
    benzBuilder.setSequence(sequence);
    BenzModel benz = (BenzModel)benzBuilder.getCarModel();
    benz.run();

    CarDirector director = new CarDirector();
    //1万辆A类型的奔驰车
    for(int i=0;i<2;i++){
    director.getABenzModel().run();
    }
    //100万辆B类型的奔驰车
    for(int i=0;i<3;i++){
    director.getBBenzModel().run();
    }
    //1000万辆C类型的宝马车
    for(int i=0;i<5;i++){
    director.getCBMWModel().run();
    }


    }
    }
文章作者: 无知的小狼
文章链接: https://bytedance.press/2019/06/30/articles/2019/06/30/1561895138748.html/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 无知的小狼

评论