考虑使用静态工厂方法来替代构造方法, 这样的做的好处有四点.

1. 更好的表意

有的构造方法实际上有特殊的含义, 使用静态工厂方法能更好的表达出他的意思. 例如 BigInteger(int, int, Random) , 它返回一个可能是素数的 BigInteger. 使用工厂方法 BigInteger.probablePrime 可以更好的表达出他的意思

2. 无需每次创建新对象

在某些场景下, 我们无需每次都创建新的对象, 这样就可以使用静态工厂方法替代构造方法, 例如 Boolean.valueOf(boolean) , 这样可以提高性能. 另一方面, 使用这种方式可以做 实例控制 (instance-controlled). 我们可以将类的实例控制在只有唯一一个, 这样在判断相等时可以使用 '==' 而不是 equals 方法, 以提升性能. Enum 类型就是这样的.

3. 可以放回任意本类的子类型

构造方法是 void 方法, 他不能有返回值. 而静态工厂方法则可以用来返回任意本类的子类型.

一个灵活的程序 API 可以放回非 public 类对象, 通过隐藏这些类对象的实现来实现 基于接口的框架 (interface-based frameworks). 接口实现对象由静态工厂方法返回. 由于接口不能有静态方法, 所以一般来说 Type 接口的实际返回类是一个名为 Types 的工具类的静态方法提供的.

例如, Java 的 Collections 框架就是这样, 它内部的便捷方法都是静态工厂方法, 并且都放回了非 public 类的实例, 而客户端无需知道他的具体实现细节, 只需要关注返回的类型接口. 例如

public static final Set EMPTY_SET = new EmptySet<>();

public static final <T> Set<T> emptySet() {
return (Set<T>) EMPTY_SET;
}

其中 EmptySet 就是一个 private static class.

此外, 静态工厂方法还可以返回方法声明中返回类型的任意子类型. 例如 EnumSet 的实现, 它的构造方法是 default 作用域, 但是它有一个静态方法 noneOf(), 这个方法会根据底层 enum 的大小来决定返回的 EnumSet 类型.

    public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
Enum[] universe = getUniverse(elementType);
if (universe == null)
throw new ClassCastException(elementType + " not an enum"); if (universe.length <= 64)
return new RegularEnumSet<>(elementType, universe);
else
return new JumboEnumSet<>(elementType, universe);
}

这样做的好处是实现对客户端隐藏, 客户端只需要关注返回的是类型是 EnumSet. 如果以后发现 RegularEnumSet 在少元素的情况下没有性能优势了, 我们可以在静态方法中把它去掉. 亦或者在静态方法中加入其他 EnumSet 的子类型.

Service provider framework

此外, 静态工厂方法可以用来实现 service provider framework, 例如 JDBC 就是这种. Service provider framework 系统由多个 service provider 实现一个 service, 并且只有系统的 service 接口对用户可见, 将 service 的实现和使用与用户解耦.

Service provider framework 的必要组件有:

  • Service 接口, 用于提供服务
  • Provider 接口, 用于实现 Service
  • Provider registration API, 用于系统注册 provider
  • Service access API, 用于客户端获取 service 实例

一般来说 Service access API 不需要强制客户端指定 provider, 如果未指定 provider, 他可以使用缺省的 provider. Service access API 就是 service provider framework 典型的 "flexible static factory".

在 JDBC 中, Connection 充当 Service 接口; Driver 充当 Provider 接口; DriverManager.registerDriver 充当 Provider Registration API; DriverManager.getConnection 充当 Service Access API.

模板代码如下

// Service provider framework sketch

// Service interface
public interface Service {
... // Service-specific methods go here
} // Service provider interface
public interface Provider {
Service newService();
}
// Noninstantiable class for service registration and access
public class Services {
private Services() { } // Prevents instantiation (Item 4)
// Maps service names to services
private static final Map<String, Provider> providers =
new ConcurrentHashMap<String, Provider>();
public static final String DEFAULT_PROVIDER_NAME = "<def>";
// Provider registration API
public static void registerDefaultProvider(Provider p) {
registerProvider(DEFAULT_PROVIDER_NAME, p);
}
public static void registerProvider(String name, Provider p){
providers.put(name, p);
}
// Service access API
public static Service newInstance() {
return newInstance(DEFAULT_PROVIDER_NAME);
}
public static Service newInstance(String name) {
Provider p = providers.get(name);
if (p == null)
throw new IllegalArgumentException(
"No provider registered with name: " + name);
return p.newService();
}
}

4. 减少了泛型类构造方法的冗长程度

泛型类的构造方法看起来总是很冗长, 例如:

Map<String, List<String>> m =
new HashMap<String, List<String>>();

使用静态工厂方法, 则可以减少这些代码

 public static <K, V> HashMap<K, V> newInstance() {
return new HashMap<K, V>();
}

在构造时可以变成这样

Map<String, List<String>> m = HashMap.newInstance();

相对的, 使用静态工厂方法也有坏处

1. 使用静态工厂方法生成的非 public protected 类无法被继承

例如在 Collections Framework 中的那些 private 类, 都无法被继承.

2. 静态工厂方法无法和其他静态方法区分开来

对于一个构造方法是私有的, 而只能通过静态工厂方法来获取实例的类, 我们无法将这个构造的静态工厂方法与其他静态类区分开来, 所以只能够 Javadoc 的注释来标记他们.

最后, 是一些常用的静态工厂方法名

  • valueOf - 返回更参数值相同的类实例
  • of - valueOf 的简写
  • getInstance - 返回一个实例, 如果不存在则创建之
  • newInstance - 类似于 getInstance, 只是每次都创建一个新实例
  • getType - 类似于 getInstance, 只是返回的实例类型由参数 Type 指定
  • newType - 类似于 newInstance, 只是返回的实例类型由参数 Type 指定

Effective Java - Item 1: Consider static factory methods instead of constructors的更多相关文章

  1. Effective Java P2 Item1 Consider static factory methods instead of constructors

    获得一个类的实例的传统方法是公共的构造方法,还可以提供一个公共的静态工厂方法(一个返回值为该类实例的简单静态方法), 例如Boolean(boolean 的封装类) public static Boo ...

  2. Effective Java 01 Consider static factory methods instead of constructors

    Advantage Unlike constructors, they have names. (BigInteger.probablePrime vs BigInteger(int, int, Ra ...

  3. 读Effective Java笔记之one:static Factory methods instead of Constructors (静态工厂方与构造器)

    获取类的实例的方法有很多种,在这很多种方法中,它们各有优缺,各有特点.这里,只介绍2中方法 1.使用构造方法 public class Person { private String sex; /** ...

  4. 《Effective Java》读书笔记 - 2.创建和销毁对象

    Chapter 2 Creating and Destroying Objects item 1:Consider static factory methods instead of construc ...

  5. Effective Java —— 用静态工厂方法代替构造器

    本文参考 本篇文章参考自<Effective Java>第三版第一条"Consider static factory methods instead of constructor ...

  6. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  7. Effective Java 第三版笔记(目录)

    <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时 ...

  8. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  9. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

随机推荐

  1. 洛谷AC200纪念

  2. 【翻译】checkbox的第三种状态

    checkbox只有两种值:选中(checked)或未选中(unchecked).它可以有任何值,但是表单提交时checkbox的值只能是checked或unchecked.它的默认值是uncheck ...

  3. Lua打印table树形结构

    --这是quick中的工具,作用就是打印Lua中强大的table的结构, 当table的嵌套层级比较多的时候,这个工具非常方便,开发中必备的工具.--具体使用方法:local debug = requ ...

  4. VMware vmdk文件打开方法

    打开虚拟机设置——硬盘——映射,把虚拟机磁盘文件vmdk映射到系统中即可!

  5. [原创] 浅谈ETL系统架构如何测试?

    [原创] 浅谈ETL系统架构如何测试? 来新公司已入职3个月时间,由于公司所处于互联网基金行业,基金天然固有特点,基金业务复杂,基金数据信息众多,基金经理众多等,所以大家可想一下,基民要想赚钱真不容易 ...

  6. Go 面试题(附答案解析)

    1.写出下面代码输出内容 package main import ( "fmt" ) func main() { defer_call() } func defer_call() ...

  7. 机房收费系统——UML用例图

    用例图(Use Case Diagram)是由软件需求分析到终于实现的第一步,说明的是谁要使用系统,以及他们使用该系统能够做些什么,是九种图里面最为基础且很重要的一张图.     用例图包含3方面内容 ...

  8. drp错误集锦---“Cannot return from outside a function or method”

    好久都不动的项目,今天打开项目突然是红色感叹号.详细错误表现为: 也就是说,如今MyEclipse已经不识别在JSP页面中使用的return方法了(并且不止一处这种警告),那怎么办?????顿时闹钟一 ...

  9. Revit API创建墙的保温层修改墙厚度

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)]  / ;         ; ...

  10. DbContextScope,A simple and flexible way to manage your Entity Framework DbContext instances,by mehdime

    DbContextScope A simple and flexible way to manage your Entity Framework DbContext instances. DbCont ...