zoukankan      html  css  js  c++  java
  • 线程安全的多参构建器实现

    适用情况:

    1:参数多

    2:需要带参构造器多(避免冗余)

    3:多线程并发情况

    /**
     * 线程安全的多参构建器实现
     *
     * @author 祥少
     *
     */
    public class Test {
        private int a;
        private int b;
        private int c;

        @Override
        public String toString() {
            return "Test [a=" + a + ", b=" + b + ", c=" + c + "]";
        }

        public Test(Build bd) {
            a = bd.a;
            b = bd.b;
            c = bd.c;
        }

        public static class Build {
            private int a;
            private int b = 0;
            private int c = 0;

            public Build(int a) {
                this.a = a;
            }

            public Build setB(int b) {
                this.b = b;
                return this;
            }

            public Build setC(int c) {
                this.c = c;
                return this;
            }

            public Test build() {
                return new Test(this);
            }
        }

        public static void main(String[] args) {
            Test test = new Test.Build(100).setB(200).build();
            System.out.println(test.toString());
        }
    }

  • 相关阅读:
    超级迷宫我的计划表
    不敢死队
    Let the Balloon Rise
    Hangover
    汉诺塔系列2
    Tri Tiling(递推)
    Tiling(递推,高精度)
    Color Me Less
    I Think I Need a Houseboat(圆计算)
    Kbased Numbers(递推)
  • 原文地址:https://www.cnblogs.com/daixiang-java/p/6789619.html
Copyright © 2011-2022 走看看