[关闭]
@XingdingCAO 2017-11-24T11:06:49.000000Z 字数 4160 阅读 1866

Effective Java(第2版):Item 3 —— Enforce the singleton property with a private constructor or an enum type

Java singleton constructor


单例的定义

singleton就是一个仅仅实例化一次的类(在任何时期,内存中都应该仅存在一个实例),称做:单例。单例通常代表了一个在本质上数量保持唯一的系统组件,如:窗口管理器、文件系统。

单例的实现

演进部分引自:http://wuchong.me/blog/2014/08/28/how-to-correctly-write-singleton-pattern/

演进过程

先不去看《Effective Java》中介绍的单例模式,先看一下单例模式的演进过程。

1. lazily initialize(unsafe)

  1. //DON'T DO THIS
  2. public class Singleton{
  3. private Singleton(){}
  4. private static Singleton INSTANCE;
  5. public static Singleton getInstance(){
  6. if(INSTANCE==null)
  7. INSTANCE = new Singleton();
  8. return INSTANCE;
  9. }
  10. }

2. lazily initialize(safe)

  1. public class Singleton{
  2. private Singleton(){}
  3. private static Singleton INSTANCE;
  4. public static synchronized Singleton getInstance(){
  5. if(INSTANCE==null)
  6. INSTANCE = new Singleton();
  7. return INSTANCE;
  8. }
  9. }

3. double-check(only safe when field is decorated with volatile after Java 5)

  1. public class Singleton{
  2. private Singleton(){}
  3. private static Singleton INSTANCE;
  4. public static Singleton getInstance(){
  5. if(INSTANCE==null){
  6. synchronized(Singleton.class)
  7. if(INSTANCE==null){
  8. INSTANCE = new Singleton();
  9. }
  10. }
  11. return INSTANCE;
  12. }
  13. }

4. static factory(not-full lazily initialize)

  1. public class Singleton{
  2. private Singleton(){}
  3. private static final Singleton INSTANCE=new Singleton();
  4. public static Singleton getInstance(){
  5. return INSTANCE;
  6. }
  7. }
  1. public class Singleton{
  2. private Singleton(){}
  3. private static final Singleton INSTANCE;
  4. static{
  5. INSTANCE = new Singleton();
  6. }
  7. public static Singleton getInstance(){
  8. return INSTANCE;
  9. }
  10. }

Effective Java(1st version)推崇的模式

5.static nealed class(full lazily initialize)

  1. public class Singleton{
  2. private static class SingletonHolder{
  3. private static Singleton INSTANCE = new Singleton();
  4. }
  5. public static Singleton getInstance(){
  6. return SingletonHolder.INSTANCE;
  7. }
  8. private Singleton(){}
  9. }

之前第一版中作者最推崇的模式就是本例中的静态内部类,实现了完全的lazy instantiation

Effective Java(2nd version)推崇的模式

(例4的前身)public static final field

  1. //Singleton with public final field
  2. public class Elvis{
  3. public static final Elvis INSTACE = new Elvis();
  4. private Elvis(){ ... }
  5. public void leaveTheBuilding(){ ... }
  6. }

(同例4)static factory

  1. //Sigleton with static factory
  2. public class Elvis{
  3. private static final Elvis INSTACE = new Elvis();
  4. private Elvis(){ ... }
  5. public static getInstace(){return INSTANCE;}
  6. public void leaveTheBuilding(){ ... }
  7. }

6. Enum

  1. public enum Elvis{
  2. INSTANCE;
  3. public void leaveTheBuilding(){ ... }
  4. }
  1. public class Elvis implement Serializable{
  2. public static final Elvis INSTACE = new Elvis();
  3. private transient int ID;
  4. private Elvis(){ ... }
  5. public void leaveTheBuilding(){ ... }
  6. @Override
  7. public Object readResolve(){
  8. return INSTANCE;
  9. }
  10. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注