自己动手实现牛逼的单例模式

其实写这篇文章之前,我犹豫了一下,毕竟单例大家都知道,写这么一篇文章会不会让人觉得老掉牙。后来想想,就当一种记录吧。先来一副漫画吧,如下图所示
image
ok,我们回顾下小灰的遭遇,上述漫画所提出的那些问题主要有以下三点:

  1. 为什么静态内部类的单例模式是最推荐的?
  2. 如何在反射的情况下保证单例?
  3. 如何在反序列化中保证单例?

针对上述三个问题有了这篇文章,以一种循序渐进的方式,引出最后一种单例设计模式,希望对大家能够有所帮助。

单例设计模式

1、饿汉式

这种其实大家都懂,不多说,上代码。


 
 
  1. package singleton;
  2. public class Singleton1 {
  3. private static Singleton1 instance = new Singleton1();
  4. private Singleton1 (){}
  5. public static Singleton1 getInstance() {
  6. return instance;
  7. }
  8. }

优点就是线程安全啦,缺点很明显,类加载的时候就实例化对象了,浪费空间。于是乎,就提出了懒汉式的单例模式

2、懒汉式

(1)懒汉式v1


 
 
  1. package singleton;
  2. public class LazySingleton1 {
  3. private static LazySingleton1 instance;
  4. private LazySingleton1 (){}
  5. public static LazySingleton1 getInstance() {
  6. if (instance == null) {
  7. instance = new LazySingleton1();
  8. }
  9. return instance;
  10. }
  11. }
 

然而这一版线程是不安全的,于是乎为了线程安全,就在getInstance()方法上加synchronized修饰符,于是getInstance()方法如下所示


 
 
  1. public static synchronized LazySingleton1 getInstance() {
  2. if (instance == null) {
  3. instance = new LazySingleton1();
  4. }
  5. return instance;
  6. }

然而,将synchronized加在方法上性能大打折扣(syncrhonized会造成线程阻塞),于是乎又提出一种双重校验锁的单例设计模式,既保证了线程安全,又提高了性能。双重校验锁的getInstance()方法如下所示


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

(2)懒汉式v2

懒汉式v1的最后一个双重校验锁版,不管性能再如何优越,还是使用了synchronized修饰符,既然使用了该修饰符,那么对性能多多少少都会造成一些影响,于是乎懒汉式v2版诞生。不过在讲该版之前,我们先来复习一下内部类的加载机制,代码如下


 
 
  1. package test;
  2. public class OuterTest {
  3. static {
  4. System.out.println( "load outer class...");
  5. }
  6. // 静态内部类
  7. static class StaticInnerTest {
  8. static {
  9. System.out.println( "load static inner class...");
  10. }
  11. static void staticInnerMethod() {
  12. System.out.println( "static inner method...");
  13. }
  14. }
  15. public static void main(String[] args) {
  16. OuterTest outerTest = new OuterTest(); // 此刻其内部类是否也会被加载?
  17. System.out.println( "===========分割线===========");
  18. OuterTest.StaticInnerTest.staticInnerMethod(); // 调用内部类的静态方法
  19. }
  20. }

输出如下


 
 
  1. load outer class...
  2. ===========分割线===========
  3. load static inner class...
  4. static inner method

因此,我们有如下结论

  1. 加载一个类时,其内部类不会同时被加载。
  2. 一个类被加载,当且仅当其某个静态成员(静态域、构造器、静态方法等)被调用时发生。。

基于上述结论,我们有了懒汉式V2版,代码如下所示


 
 
  1. package singleton;
  2. public class LazySingleton2 {
  3. private LazySingleton2() {
  4. }
  5. static class SingletonHolder {
  6. private static final LazySingleton2 instance = new LazySingleton2();
  7. }
  8. public static LazySingleton2 getInstance() {
  9. return SingletonHolder.instance;
  10. }
  11. }

由于对象实例化是在内部类加载的时候构建的,因此该版是线程安全的(因为在方法中创建对象,才存在并发问题,静态内部类随着方法调用而被加载,只加载一次,不存在并发问题,所以是线程安全的)。另外,在getInstance()方法中没有使用synchronized关键字,因此没有造成多余的性能损耗。当LazySingleton2类加载的时候,其静态内部类SingletonHolder并没有被加载,因此instance对象并没有构建。而我们在调用LazySingleton2.getInstance()方法时,内部类SingletonHolder被加载,此时单例对象才被构建。因此,这种写法节约空间,达到懒加载的目的,该版也是众多博客中的推荐版本。

ps:其实枚举单例模式也有类似的性能,但是因为可读性的原因,并不是最推荐的版本。

(3)懒汉式v3

然而,懒汉式v2版在反射的作用下,单例结构是会被破坏的,测试代码如下所示


 
 
  1. package test;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.InvocationTargetException;
  4. import singleton.LazySingleton2;
  5. public class LazySingleton2Test {
  6. public static void main(String[] args) {
  7. //创建第一个实例
  8. LazySingleton2 instance1 = LazySingleton2.getInstance();
  9. //通过反射创建第二个实例
  10. LazySingleton2 instance2 = null;
  11. try {
  12. Class<LazySingleton2> clazz = LazySingleton2.class;
  13. Constructor<LazySingleton2> cons = clazz.getDeclaredConstructor();
  14. cons.setAccessible( true);
  15. instance2 = cons.newInstance();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. //检查两个实例的hash值
  20. System.out.println( "Instance 1 hash:" + instance1.hashCode());
  21. System.out.println( "Instance 2 hash:" + instance2.hashCode());
  22. }
  23. }

输出如下


 
 
  1. Instance 1 hash :1694819250
  2. Instance 2 hash :1365202186

根据哈希值可以看出,反射破坏了单例的特性,因此懒汉式V3版诞生了


 
 
  1. package singleton;
  2. public class LazySingleton3 {
  3. private static boolean initialized = false;
  4. private LazySingleton3() {
  5. synchronized (LazySingleton3.class) {
  6. if (initialized == false) {
  7. initialized = !initialized;
  8. } else {
  9. throw new RuntimeException( "单例已被破坏");
  10. }
  11. }
  12. }
  13. static class SingletonHolder {
  14. private static final LazySingleton3 instance = new LazySingleton3();
  15. }
  16. public static LazySingleton3 getInstance() {
  17. return SingletonHolder.instance;
  18. }
  19. }

此时再运行一次测试类,出现如下提示


 
 
  1. java .lang .reflect .InvocationTargetException
  2. at sun .reflect .NativeConstructorAccessorImpl .newInstance0( Native Method)
  3. at sun .reflect .NativeConstructorAccessorImpl .newInstance( NativeConstructorAccessorImpl .java :62)
  4. at sun .reflect .DelegatingConstructorAccessorImpl .newInstance( DelegatingConstructorAccessorImpl .java :45)
  5. at java .lang .reflect .Constructor .newInstance( Constructor .java :423)
  6. at test .LazySingleton3Test .main( LazySingleton3Test .java :21)
  7. Caused by: java .lang .RuntimeException: 单例已被破坏
  8. at singleton .LazySingleton3.< init>( LazySingleton3 .java :12)
  9. ... 5 more
  10. Instance 1 hash :359023572

这里就保证了,反射无法破坏其单例特性

(3)懒汉式v4

在分布式系统中,有些情况下你需要在单例类中实现 Serializable 接口。这样你可以在文件系统中存储它的状态并且在稍后的某一时间点取出。

让我们测试这个懒汉式V3版在序列化和反序列化之后是否仍然保持单例。

先将

public class LazySingleton3
 
 

修改为

public class LazySingleton3 implements Serializable 
 
 

上测试类如下


 
 
  1. package test;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.ObjectInput;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutput;
  7. import java.io.ObjectOutputStream;
  8. import singleton.LazySingleton3;
  9. public class LazySingleton3Test {
  10. public static void main(String[] args) {
  11. try {
  12. LazySingleton3 instance1 = LazySingleton3.getInstance();
  13. ObjectOutput out = null;
  14. out = new ObjectOutputStream( new FileOutputStream( "filename.ser"));
  15. out.writeObject(instance1);
  16. out.close();
  17. //deserialize from file to object
  18. ObjectInput in = new ObjectInputStream( new FileInputStream( "filename.ser"));
  19. LazySingleton3 instance2 = (LazySingleton3) in.readObject();
  20. in.close();
  21. System.out.println( "instance1 hashCode=" + instance1.hashCode());
  22. System.out.println( "instance2 hashCode=" + instance2.hashCode());
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }

输出如下


 
 
  1. instance1 hashCode=2051450519
  2. instance2 hashCode=1510067370

显然,我们又看到了两个实例类。为了避免此问题,我们需要提供 readResolve() 方法的实现。readResolve()代替了从流中读取对象。这就确保了在序列化和反序列化的过程中没人可以创建新的实例。

因此,我们提供懒汉式V4版代码如下


 
 
  1. package singleton;
  2. import java.io.Serializable;
  3. public class LazySingleton4 implements Serializable {
  4. private static boolean initialized = false;
  5. private LazySingleton4() {
  6. synchronized (LazySingleton4.class) {
  7. if (initialized == false) {
  8. initialized = !initialized;
  9. } else {
  10. throw new RuntimeException( "单例已被破坏");
  11. }
  12. }
  13. }
  14. static class SingletonHolder {
  15. private static final LazySingleton4 instance = new LazySingleton4();
  16. }
  17. public static LazySingleton4 getInstance() {
  18. return SingletonHolder.instance;
  19. }
  20. private Object readResolve() {
  21. return getInstance();
  22. }
  23. }

此时,在运行测试类,输出如下


 
 
  1. instance1 hashCode=2051450519
  2. instance2 hashCode=2051450519

这表示此时已能保证序列化和反序列化的对象是一致的

总结

本文给出了多个版本的单例模式,供我们在项目中使用。实际上,我们在实际项目中一般从懒汉式v2、懒汉式v3、懒汉式v4中,根据实际情况三选一即可,并不是非要选择懒汉式v4作为单例来实现。
最后,希望大家有所收获。

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值