当前位置:   article > 正文

设计模式-构造者模式

设计模式-构造者模式

案例引入

假如我们有下面这个config,其中必填的参数有很多,非必填的参数也有几个,maxThread和cpuCore 存在依赖关系。我们可以通过构造参数 + set 来进行赋值操作, 可以实现我们的功能。

public class VirtualHostConfig {
    /**
     * 构造函数
     */
    public VirtualHostConfig(String host, int port, int cpuCore, int diskSize, int netWorkTraffic, int maxThread, boolean allowSsh) {
        this.host = host;
        this.port = port;
        this.cpuCore = cpuCore;
        this.diskSize = diskSize;
        this.netWorkTraffic = netWorkTraffic;
        this.allowSsh = allowSsh;
        this.maxThread = maxThread;
        if (StrUtil.isBlank(host)) {
            System.out.println("host must fill");
            throw new RuntimeException("host must fill");
        }
        if (this.port <= 0 || this.port >= 65535) {
            System.out.println("port invalid");
            throw new RuntimeException("port invalid");
        }
        if (this.cpuCore <= 0 || this.cpuCore > 8) {
            System.out.println("cpu core must lt 8");
            throw new RuntimeException("cpu core must lt 8");
        }
        if (this.maxThread <= 0 || this.maxThread > 8) {
            System.out.println("maxThread must lt 8");
            throw new RuntimeException("maxThread must lt 8");
        }
        if (this.diskSize < 256 || this.diskSize > 1024 || this.diskSize % 8 != 0) {
            System.out.println("diskSize not valid");
            throw new RuntimeException("diskSize not valid");
        }

        if (this.netWorkTraffic < 1 || this.netWorkTraffic > 10) {
            System.out.println("netWorkTraffic invalid");
            throw new RuntimeException("netWorkTraffic invalid");
        }
        // 存在依赖关系
        if (this.maxThread > this.cpuCore) {
            System.out.println("maxThread invalid");
            throw new RuntimeException("maxThread invalid");
        }

    }
    /**
     *  必填 主机名称
     */
    private String host;
    /**
     *  必填 0~65535
     */
    private int port = -1;

    /**
     * 非必填 但是不能超过 cpuCore
     */
    private int maxThread;

    /**
     * 必填 CPU 不超过8 填完不允许修改
     */
    private int cpuCore = -1;


    /**
     * 非必填 缓存 不超过 512
     */
    private int cacheSize = -1;

    /**
     *  非必填 服务器别名
     */
    private String alisaName;

    /**
     * 必填 磁盘大小
     */
    private int diskSize = -1;

    /**
     * 必填 网络带宽
     */
    private int netWorkTraffic = -1;

    /**
     * 必填 是否允许ssh
     */
    private boolean allowSsh;

    public int getMaxThread() {
        return maxThread;
    }

    public void setMaxThread(int maxThread) {
        this.maxThread = maxThread;
    }

    public int getCacheSize() {
        return cacheSize;
    }

    public void setCacheSize(int cacheSize) {
        this.cacheSize = cacheSize;
    }

    public String getAlisaName() {
        return alisaName;
    }

    public void setAlisaName(String alisaName) {
        this.alisaName = alisaName;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public int getCpuCore() {
        return cpuCore;
    }

    public int getDiskSize() {
        return diskSize;
    }

    public int getNetWorkTraffic() {
        return netWorkTraffic;
    }

    public boolean isAllowSsh() {
        return allowSsh;
    }
}
  • 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

现在有这样几个需求:
1 要求这个config一旦填好了以后不可以修改。
2 公司要求参数方法的参数列表不可超过5个,否则扣钱。

为了满足2这个时候我们可以也可以把必填的参数通过set的方式来注入,但是这样违背了1,同样之前非必填通过set注入也违背了1.并且如果通过set注入的话,我们的校验逻辑也没有地方放置。这个时候构造模式来拯救我们了。

构造者模式

public class VirtualHostConfig {
    /**
     *  必填 主机名称
     */
    private String host;
    /**
     *  必填 0~65535
     */
    private int port = -1;

    /**
     * 非必填 但是不能超过 cpuCore
     */
    private int maxThread;

    /**
     * 必填 CPU 不超过8 填完不允许修改
     */
    private int cpuCore = -1;


    /**
     * 非必填 缓存 不超过 512
     */
    private int cacheSize = -1;

    /**
     *  非必填 服务器别名
     */
    private String alisaName;

    /**
     * 必填 磁盘大小
     */
    private int diskSize = -1;

    /**
     * 必填 网络带宽
     */
    private int netWorkTraffic = -1;

    /**
     * 必填 是否允许ssh
     */
    private boolean allowSsh;
    public VirtualHostConfig(Builder builder) {
        this.host = builder.host;
        this.port = builder.port;
        this.cpuCore = builder.cpuCore;
        this.diskSize = builder.diskSize;
        this.netWorkTraffic = builder.netWorkTraffic;
        this.allowSsh = builder.allowSsh;
        this.alisaName = builder.alisaName;
        this.cacheSize = builder.cacheSize;
        this.maxThread = builder.maxThread;
    }
    public int getMaxThread() {
        return maxThread;
    }

    public void setMaxThread(int maxThread) {
        this.maxThread = maxThread;
    }

    public int getCacheSize() {
        return cacheSize;
    }

    public void setCacheSize(int cacheSize) {
        this.cacheSize = cacheSize;
    }

    public String getAlisaName() {
        return alisaName;
    }

    public void setAlisaName(String alisaName) {
        this.alisaName = alisaName;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public int getCpuCore() {
        return cpuCore;
    }

    public int getDiskSize() {
        return diskSize;
    }

    public int getNetWorkTraffic() {
        return netWorkTraffic;
    }

    public boolean isAllowSsh() {
        return allowSsh;
    }
    static class Builder {
        /**
         *  必填 主机名称
         */
        private String host;
        /**
         *  必填 0~65535
         */
        private int port = -1;

        /**
         * 非必填 但是不能超过 cpuCore
         */
        private int maxThread;

        /**
         * 必填 CPU 不超过8 填完不允许修改
         */
        private int cpuCore = -1;


        /**
         * 非必填 缓存 不超过 512
         */
        private int cacheSize = -1;

        /**
         *  非必填 服务器别名
         */
        private String alisaName;

        /**
         * 必填 磁盘大小
         */
        private int diskSize = -1;

        /**
         * 必填 网络带宽
         */
        private int netWorkTraffic = -1;

        /**
         * 必填 是否允许ssh
         */
        private boolean allowSsh;
        public Builder() {

        }
        public Builder host(String host) {
            this.host = host;
            return this;
        }
        public Builder alisaName(String alisaName) {
            this.alisaName = alisaName;
            return this;
        }
        public Builder port(int port) {
            this.port = port;
            return this;
        }
        public Builder diskSize(int diskSize) {
            this.diskSize = diskSize;
            return this;
        }
        public Builder cacheSize(int cacheSize) {
            this.cacheSize = cacheSize;
            return this;
        }
        public Builder netWorkTraffic(int netWorkTraffic) {
            this.netWorkTraffic = netWorkTraffic;
            return this;
        }
        public Builder cpuCore(int cpuCore) {
            this.cpuCore = cpuCore;
            return this;
        }
        public Builder allowSsh(boolean allowSsh) {
            this.allowSsh = allowSsh;
            return this;
        }
        public Builder maxThread(int maxThread) {
            this.maxThread = maxThread;
            return this;
        }
        public VirtualHostConfig build() {
            if (StrUtil.isBlank(host)) {
                System.out.println("host must fill");
                throw new RuntimeException("host must fill");
            }
            if (this.port <= 0 || this.port >= 65535) {
                System.out.println("port invalid");
                throw new RuntimeException("port invalid");
            }
            if (this.cpuCore <= 0 || this.cpuCore > 8) {
                System.out.println("cpu core must lt 8");
                throw new RuntimeException("cpu core must lt 8");
            }
            if (this.maxThread <= 0 || this.maxThread > 8) {
                System.out.println("maxThread must lt 8");
                throw new RuntimeException("maxThread must lt 8");
            }
            if (this.diskSize < 256 || this.diskSize > 1024 || this.diskSize % 8 != 0) {
                System.out.println("diskSize not valid");
                throw new RuntimeException("diskSize not valid");
            }

            if (this.netWorkTraffic < 1 || this.netWorkTraffic > 10) {
                System.out.println("netWorkTraffic invalid");
                throw new RuntimeException("netWorkTraffic invalid");
            }
            // 存在依赖关系
            if (this.maxThread > this.cpuCore) {
                System.out.println("maxThread invalid");
                throw new RuntimeException("maxThread invalid");
            }
            return new VirtualHostConfig(this);
        }
    }
}
  • 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
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222

是不是感觉这种写法更好呢?这样设置了属性以后就不可以被修改了。

public static void main(String[] args) {
    VirtualHostConfig config = new Builder()
            .host("127.0.0.1")
            .cpuCore(8)
            .netWorkTraffic(2)
            .port(8989)
            .diskSize(512)
            .cacheSize(128)
            .alisaName("server1")
            .allowSsh(false)
            .maxThread(7)
            .build();
    System.out.println(JSONUtil.toJsonStr(config));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/171997
推荐阅读
相关标签
  

闽ICP备14008679号