美文网首页Java
Java单例模式实现,一次性学完整,面试加分项

Java单例模式实现,一次性学完整,面试加分项

作者: java涛仔 | 来源:发表于2021-12-15 14:42 被阅读0次

    单例模式是设计模式中使用最为普遍的一种模式。属于对象创建模式,它可以确保系统中 一个类 只产生 一个实例。这样的行为能带来两大好处:

    对于频繁使用的对象,可以省略创建对象所花费的时间,这对于那些重量级对象而言,是非常可观的一笔系统开销。

    由于new操作的次数减少,因而对系统内存的使用频率也会降低,这将减轻GC压力,缩短GC停顿时间。

    在实际应用中,很多时候有一些对象我们只需要一个,例如:线程池(threadpool)、缓存(cache)、注册表(registry)、日志对象等等,这个时候把它设计为单例模式是最好的选择。

    1、单例模式6种实现方法

    1)懒汉模式(线程不安全)

    publicclassSingleton01{privatestaticSingleton01 instance;/**

        * 私有构造方法

        */privateSingleton01(){}publicstaticSingleton01getInstance(){if(instance ==null) {            instance =newSingleton01();        }returninstance;    }}

    这种写法实现延迟加载,但线程不安全。禁止使用!

    2)懒汉模式(线程安全)

    publicclassSingleton02{privatestaticSingleton02 instance;/**

        * 私有构造方法

        */privateSingleton02(){}publicstaticsynchronized Singleton02getInstance(){if(instance ==null) {            instance =newSingleton02();        }returninstance;    }}

    这种写法实现延迟加载,且增加synchronized来保证线程安全,但效率太低。不建议使用

    3)懒汉模式(双重校验锁)

    publicclassSingleton03{privatevolatilestaticSingleton03 instance;/**

        * 私有构造方法

        */privateSingleton03(){}publicstaticSingleton03getInstance(){if(instance ==null) {synchronized(Singleton03.class){if(instance ==null) {                    instance =newSingleton03();                }            }        }returninstance;    }}

    使用到了volatile机制。这个是第二种方式的升级版,俗称双重检查锁定。既保证了效率,又保证了安全。

    4)饿汉模式

    publicclassSingleton03{privatestaticSingleton03 instance =newSingleton03();/**

        * 私有构造方法

        */privateSingleton03(){}publicstaticsynchronized Singleton03getInstance(){returninstance;    }}

    这种基于类加载机制避免了多线程的同步问题,初始化的时候就给装载了。但却没了懒加载的效果。

    这也是最简单的一种实现。

    5)静态内部类

    publicclassSingleton04{// 静态内部类privatestaticclassSingletonHolder{privatestaticfinalSingleton04 INSTANCE =newSingleton04();    }/**

        * 私有构造方法

        */privateSingleton04(){}publicstaticSingleton04getInstance(){returnSingletonHolder.INSTANCE;    }}

    这种方式当Singleton04类被加载时,其内部类并不会被加载,所以单例类INSTANCE不会被初始化。

    只有显式调用getInstance方法时,才会加载SingletonHolder,从而实例化INSTANCE。

    由于实例的建立是在类加载时完成,所以天生线程安全。因此兼备了懒加载和线程安全的特性。

    6)枚举(号称最好)

    publicenumEnumSingleton01 {    INSTANCE;publicvoiddoSomething(){        System.out.println("doSomething");    }}

    模拟数据库链接:

    publicenumEnumSingleton02 {    INSTANCE;privateDBConnection dbConnection =null;privateEnumSingleton02(){        dbConnection =newDBConnection();    }publicDBConnectiongetConnection(){returndbConnection;    }}

    这种方式是Effective Java作者Josh Bloch提倡的方式,它不仅能避免多线程同步问题,

    而且还能防止反序列化重新创建新的对象。

    2、为什么说枚举方法是最好的?

    前5种方式实现单例都有如下3个特点:

    构造方法私有化

    实例化的变量引用私有化

    获取实例的方法共有

    首先,私有化构造器并不保险。因为它抵御不了反射攻击,其次就是序列化重新创建新对象。下面来进行验证。

    1) 反射验证

    @TestpublicvoidreflectTest() 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

    再看看枚举类的测试

    @TestpublicvoidreflectEnumTest() 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.()atjava.lang.Class.getConstructor0(Class.java:3082)atjava.lang.Class.getDeclaredConstructor(Class.java:2178)atorg.yd.singleton.SingletonTest.reflectEnumTest(SingletonTest.java:61)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    结论:通过反射,单例模式的私有构造方法也能构造出新对象。不安全。而枚举类直接抛异常,说明枚举类对反射是安全的。

    2) 序列化验证

    @Testpublicvoid 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

    结论:序列化前后两个对象并不相等。所以序列化也是不安全的。

    同样看看枚举类的测试

    @Testpublicvoid 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

    结论:说明枚举类序列化安全。

    综上,可以得出结论:枚举是实现单例模式的最佳实践。

    反射安全

    序列化/反序列化安全

    写法简单

    相关文章

      网友评论

        本文标题:Java单例模式实现,一次性学完整,面试加分项

        本文链接:https://www.haomeiwen.com/subject/ikhpfrtx.html