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());
        }
    }

  • 相关阅读:
    1015,存储过程,视图
    1009,数据库查询,聚合函数,日期时间函数
    1008,数据库表格创建,名称,格式

    公历和农历转换的JS代码
    面向对象之封装
    HTML之锚点
    HTML之css+div
    HTML基础
    SQL之定义变量
  • 原文地址:https://www.cnblogs.com/daixiang-java/p/6789619.html
Copyright © 2011-2022 走看看