博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Effective Java
阅读量:6705 次
发布时间:2019-06-25

本文共 3123 字,大约阅读时间需要 10 分钟。

考虑使用静态代码块

/** * 静态代码块 * * @author iHelin * @since 2017/11/27 14:21 */public class BabyBoomer {    private static Date birthDate;    private static final Date BOOM_START;    private static final Date BOOM_END;    static {        Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));        gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);        BOOM_START = gmtCal.getTime();        gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);        BOOM_END = gmtCal.getTime();    }    public static boolean isBabyBoomer() {        return birthDate.compareTo(BOOM_START) >= 0 && birthDate.compareTo(BOOM_END) < 0;    }    public static void main(String[] args) {        Calendar cal = Calendar.getInstance();        cal.set(1947, Calendar.FEBRUARY, 1, 0, 0, 0);        birthDate = cal.getTime();        System.out.println(isBabyBoomer());    }}复制代码

BOOM_STARTBOOM_END不变,考虑使用静态常量,然后使用静态代码块初始化。

使用枚举实现单例

/** * 单例 - 使用枚举实现 * * @author iHelin * @since 2017/11/27 14:27 */public class Elvis {    private static final Elvis INSTANCE = new Elvis();    private Elvis() {    }    public static Elvis getInstance() {        return INSTANCE;    }    public Object readResolve() {        return INSTANCE;    }}复制代码

遇到多个构造器参数时考虑用构建器

/** * 遇到多个构造器参数时考虑用构建器 * * @author iHelin * @since 2017/11/27 14:24 */public class NutritionFacts {    private final int servingSize;    private final int servings;    private final int calories;    private final int fat;    private final int sodium;    private final int carbohydrate;    public static class Builder {        /**         * Required parameters         */        private final int servingSize;        private final int servings;        /**         * Optional parameters         */        private int calories = 0;        private int fat = 0;        private int sodium = 0;        private int carbohydrate = 0;        public Builder(int servingSize, int servings) {            this.servingSize = servingSize;            this.servings = servings;        }        public Builder calories(int val) {            calories = val;            return this;        }        public Builder fat(int val) {            fat = val;            return this;        }        public Builder carbohydrate(int val) {            carbohydrate = val;            return this;        }        public Builder sodium(int val) {            sodium = val;            return this;        }        public NutritionFacts build() {            return new NutritionFacts(this);        }    }    private NutritionFacts(Builder builder) {        servingSize = builder.servingSize;        servings = builder.servings;        calories = builder.calories;        fat = builder.fat;        sodium = builder.sodium;        carbohydrate = builder.carbohydrate;    }    public static void main(String[] args) {        NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();        System.out.println(cocaCola);    }    @Override    public String toString() {        return ToStringBuilder.reflectionToString(this);    }}复制代码

转载于:https://juejin.im/post/5a3cdd58f265da433562f4ad

你可能感兴趣的文章
from disk cache 与 from memory cache
查看>>
应用图片加载服务与第三方实现库的解耦
查看>>
高并发的核心技术-幂等的实现方案
查看>>
微波炉炖蛋
查看>>
C#调用C/C++ DLL 参数传递和回调函数的总结
查看>>
非spring组件servlet、filter、interceptor中注入spring bean
查看>>
SQL Server中SELECT会真的阻塞SELECT吗?
查看>>
v-for设置键值 key
查看>>
通过一次SpringBoot打成war包部署到tomcat启动总结一般jar包冲突的解决方法
查看>>
class path and classloader
查看>>
文字检测与识别 资源
查看>>
外包筛选心得
查看>>
Warning: skipping non-radio button in group
查看>>
dotnet检测类型是否为泛型
查看>>
Android 悬浮窗权限校验
查看>>
使用CefSharp在.Net程序中嵌入Chrome浏览器(九)——性能问题
查看>>
mysql 创建数据库 并设置utf8格式
查看>>
IDA 逆向工程 反汇编使用
查看>>
CentOS7单独安装Apache Bench压力测试工具
查看>>
python植入后门backdoor程序的方法?
查看>>