本文共 5123 字,大约阅读时间需要 17 分钟。
单例模式是设计模式中使用最为普遍的一种模式。属于对象创建模式,它可以确保系统中一个类只产生一个实例。这样的行为能带来两大好处:
对于频繁使用的对象,可以省略创建对象所花费的时间,这对于那些重量级对象而言,是非常可观的一笔系统开销。
由于new操作的次数减少,因而对系统内存的使用频率也会降低,这将减轻GC压力,缩短GC停顿时间。
在实际应用中,很多时候有一些对象我们只需要一个,例如:线程池(threadpool)、缓存(cache)、注册表(registry)、日志对象等等,这个时候把它设计为单例模式是最好的选择。
public class Singleton01 { private static Singleton01 instance; /** * 私有构造方法 */ private Singleton01(){} public static Singleton01 getInstance() { if(instance == null) { instance = new Singleton01(); } return instance; }}
public class Singleton02 { private static Singleton02 instance; /** * 私有构造方法 */ private Singleton02(){} public static synchronized Singleton02 getInstance() { if(instance == null) { instance = new Singleton02(); } return instance; }}
public class Singleton03 { private volatile static Singleton03 instance; /** * 私有构造方法 */ private Singleton03(){} public static Singleton03 getInstance() { if(instance == null) { synchronized (Singleton03.class) { if (instance == null) { instance = new Singleton03(); } } } return instance; }}
使用到了volatile机制。这个是第二种方式的升级版,俗称双重检查锁定。既保证了效率,又保证了安全。
public class Singleton03 { private static Singleton03 instance = new Singleton03(); /** * 私有构造方法 */ private Singleton03(){} public static synchronized Singleton03 getInstance() { return instance; }}
public class Singleton04 { // 静态内部类 private static class SingletonHolder { private static final Singleton04 INSTANCE = new Singleton04(); } /** * 私有构造方法 */ private Singleton04(){} public static Singleton04 getInstance() { return SingletonHolder.INSTANCE; }}
Singleton04
类被加载时,其内部类并不会被加载,所以单例类 INSTANCE
不会被初始化。只有显式调用 getInstance
方法时,才会加载 SingletonHolder
,从而实例化 INSTANCE
。由于实例的建立是在类加载时完成,所以天生线程安全。因此兼备了懒加载和线程安全的特性。public enum EnumSingleton01 { INSTANCE; public void doSomething() { System.out.println("doSomething"); }}
public enum EnumSingleton02 { INSTANCE; private DBConnection dbConnection = null; private EnumSingleton02() { dbConnection = new DBConnection(); } public DBConnection getConnection() { return dbConnection; }}
Effective Java
作者 Josh Bloch
提倡的方式,它不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。前5种方式实现单例都有如下3个特点:
构造方法私有化
实例化的变量引用私有化
获取实例的方法共有
反射攻击
,其次就是序列化重新创建新对象。下面来进行验证。@Testpublic void reflectTest() throws Exception { Singleton03 s = Singleton03.getInstance(); // 拿到所有的构造函数,包括非public的 Constructorconstructor = Singleton03.class.getDeclaredConstructor(); constructor.setAccessible(true); // 构造实例 Singleton03 reflection = constructor.newInstance(); System.out.println(s); System.out.println(reflection); System.out.println(s == reflection);}
输出结果:
org.yd.singleton.Singleton03@61e4705borg.yd.singleton.Singleton03@50134894false
再看看枚举类的测试
@Testpublic void reflectEnumTest() throws Exception { EnumSingleton01 s = EnumSingleton01.INSTANCE; // 拿到所有的构造函数,包括非public的 Constructorconstructor = EnumSingleton01.class.getDeclaredConstructor(); constructor.setAccessible(true); // 构造实例 EnumSingleton01 reflection = constructor.newInstance(); System.out.println(s); System.out.println(reflection); System.out.println(s == reflection);}
输出结果:
java.lang.NoSuchMethodException: org.yd.singleton.EnumSingleton01.() at java.lang.Class.getConstructor0(Class.java:3082) at java.lang.Class.getDeclaredConstructor(Class.java:2178) at org.yd.singleton.SingletonTest.reflectEnumTest(SingletonTest.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
@Testpublic void serializeTest(){ Singleton03 s = Singleton03.getInstance(); String serialize = JSON.toJSONString(s); Singleton03 deserialize =JSON.parseObject(serialize,Singleton03.class); System.out.println(s); System.out.println(deserialize); System.out.println(s == deserialize);}
输出结果:
org.yd.singleton.Singleton03@387c703borg.yd.singleton.Singleton03@75412c2ffalse
同样看看枚举类的测试
@Testpublic void serializeEnumTest(){ EnumSingleton01 s = EnumSingleton01.INSTANCE; String serialize = JSON.toJSONString(s); EnumSingleton01 deserialize =JSON.parseObject(serialize,EnumSingleton01.class); System.out.println(s); System.out.println(deserialize); System.out.println(s == deserialize);}
输出结果:
INSTANCEINSTANCEtrue
综上,可以得出结论:枚举是实现单例模式的最佳实践。
反射安全
序列化/反序列化安全
写法简单
转载地址:http://djaai.baihongyu.com/