EffectiveJava之创建和销毁对象篇

您所在的位置:网站首页 effectiveJAVA EffectiveJava之创建和销毁对象篇

EffectiveJava之创建和销毁对象篇

2023-04-06 22:26| 来源: 网络整理| 查看: 265

本文正在参加「金石计划」

小伙伴们好呀,我是 4ye ,今天来和大家分享下 《Effective Java》这本书的 第2章 —— 创建和销毁对象 。

一共有 9 点,一起看看叭~

1. 考虑用 静态工厂方法 而不是构造器

有这五个优点:

有方法名 不用每次都创建对象 可以返回任意子类 可以根据不同的入参而返回不同的类 在编写包含方法的类时,返回对象的类不需要存在 A. 有方法名

这个优点确实很显眼 ,毕竟构造器名固定了🐖

B. 不用每次都创建对象

这个也好理解,可以缓存对象, 设计思想上可参考 亨元设计模式

例如 valueOf 方法 👇

BigInteger

Boolean

C. 可以返回任意子类

这个作者举了 Collections 这个工具类,但是我也没啥特别的感觉,感觉和 面向接口编程 差不多的意思

D 可以根据不同的入参而返回不同的类

这个可以参考 Spring 中 BeanFactory 接口的 getBean 方法。

这个我便轻车熟路了,它可以很对业务模块进行解耦,方便扩展

接口A a = applicationContext.getBean(参数); a.common(); 复制代码 E. 在编写包含方法的类时,返回对象的类不需要存在

这个还是 面向接口编程 好吧……

作者举了 JDBC 这个例子。

Connection conn=DriverManager.getConnection(xxx) 复制代码 2. 如果构造器有很多参数,建议用 builder 去创建对象

这个就是 建设者模式 的使用了,下面是作者 Joshua Bloch 在 GitHub 仓库给的例子

package effectivejava.chapter2.item2.builder; // Builder Pattern (Page 13) 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 - initialized to default values 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 sodium(int val) { sodium = val; return this; } public Builder carbohydrate(int val) { carbohydrate = 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(); } } 复制代码

看到这个 链式调用,不得不感叹下 stream 源码,封装的超级牛,要 List,要 Map 等都可以很随意的转换出来!👍

Builder 设计模式步骤解析

3. 用 枚举或者私有构造器 来强化 单例属性

这个就差不多在说 单例模式 了

👉 一文带你看遍单例模式的八个例子,面试再也不怕被问了 (我都忘光了😂)

这两个也是作者 Joshua Bloch 在 GitHub 仓库给的例子

枚举 👇

package effectivejava.chapter2.item3.enumtype; // Enum singleton - the preferred approach (Page 18) public enum Elvis { INSTANCE; public void leaveTheBuilding() { System.out.println("Whoa baby, I'm outta here!"); } // This code would normally appear outside the class! public static void main(String[] args) { Elvis elvis = Elvis.INSTANCE; elvis.leaveTheBuilding(); } } 复制代码

属性 👇

package effectivejava.chapter2.item3.field; // Singleton with public final field (Page 17) public class Elvis { public static final Elvis INSTANCE = new Elvis(); private Elvis() { } public void leaveTheBuilding() { System.out.println("Whoa baby, I'm outta here!"); } // This code would normally appear outside the class! public static void main(String[] args) { Elvis elvis = Elvis.INSTANCE; elvis.leaveTheBuilding(); } } 复制代码 4. 用 私有构造器 来限制实例化

这个也是 单例模式 的影子了。

5. 通过 依赖注入 而不是 硬编码 的方式使用资源

这句话听着还有点别扭,作者举了一个例子,就是一个工具里把 字典 写死了,这样是不对的,应该是下面这种依赖注入的写法才对。

个人感觉。。。还是 面向接口编程 🐖

// Dependency injection provides flexibility and testability public class SpellChecker { private final Lexicon dictionary; public SpellChecker(Lexicon dictionary) { this.dictionary = Objects.requireNonNull(dictionary); } public boolean isValid(String word) { ... } public List suggestions(String typo) { ... } } 复制代码 6. 避免创建不必要的对象

比如,String 对象的创建

// 这样写每次都创建新对象,不要使用 String s= new String("Java4ye"); // 使用 String s= "Java4ye"; 复制代码

开头提到的 亨元模式 ,valueOf 等缓存对象的方法。

日常中各种连接的建立,线程池的使用等等。

注意 基本数据类型和包装类型

// Hideously slow! Can you spot the object creation? private static long sum() { Long sum = 0L; for (long i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3