一、什么是单例模式
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
注意
单例模式只能由一个实例对象 单例类必须自己创建自己的唯一实例。 单例类必须给所有其他对象提供这一实例。
二、哪些地方用到了单例模式
单例模式经常用在需要一个实例的程序中,例如
1.Spring框架IOC容器就使用到了单例模式,默认创建对象的时候为单例模式
2.ResultBean 后端统一返回给前端的封装类,这个在项目中是唯一的,只用一个对象进行返回JSON给前端进行渲染
JDK中也有单例模式的身影,例
Runtime 体现了饿汉式单例 Console 体现了双检锁懒汉式单例 Collections 中的 EmptyNavigableSet 内部类懒汉式单例 ReverseComparator.REVERSE_ORDER 内部类懒汉式单例 Comparators.NaturalOrderComparator.INSTANCE 枚举饿汉式单例
三、单例模式的优缺点
优点
提供了对唯一实例的访问
可以节约系统资源,提高系统的性能,减少不必要的内存开销
允许可变数目的实例(多例类)
缺点
扩展困难(缺少抽象层)
单例类的职责过重
由于自动垃圾回收机制,可能会导致共享的单例对象的状态丢失
四、手写单例模式
饿汉式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.wanshi.single;
//饿汉式单例 public class Hungry {
//会造成资源浪费,占用CPU private byte [] data1 = new byte [ 1024 * 1024 ]; private byte [] data2 = new byte [ 1024 * 1024 ]; private byte [] data3 = new byte [ 1024 * 1024 ]; private byte [] data4 = new byte [ 1024 * 1024 ];
private Hungry() { System.out.println( "Hungry init..." ); }
private static Hungry hungry = new Hungry();
public static Hungry getInstance() { return hungry; } }
class Test { public static void main(String[] args) { Hungry hungry = Hungry.getInstance(); Hungry hungry2 = Hungry.getInstance(); System.out.println(hungry); System.out.println(hungry2); } } |
枚举饿汉式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.wanshi.single;
import java.lang.reflect.Constructor;
// enum 是一个class类 public enum EnumSingle {
INSTANCE;
public static EnumSingle getInstance() { return INSTANCE; } }
class Test {
public static void main(String[] args) throws Exception{ EnumSingle instance1 = EnumSingle.INSTANCE; Constructor<EnumSingle> declaredConstructor = EnumSingle. class .getDeclaredConstructor(String. class , int . class ); declaredConstructor.setAccessible( true );
EnumSingle instance2 = declaredConstructor.newInstance();
System.out.println(instance1); System.out.println(instance2); } } |
在这里自行下载jad编译工具即可
枚举类最后反编译源码
jad工具反编译
jad -sjava EnumSingle.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://HdhCmsTestkpdus测试数据/jad.html // Decompiler options: packimports(3) // Source File Name: EnumSingle.java
package com.wanshi.single;
public final class EnumSingle extends Enum {
public static EnumSingle[] values() { return (EnumSingle[])$VALUES.clone(); }
public static EnumSingle valueOf(String name) { return (EnumSingle)Enum.valueOf(com/wanshi/single/EnumSingle, name); }
private EnumSingle(String s, int i) { super (s, i); }
public static EnumSingle getInstance() { return INSTANCE; }
public static final EnumSingle INSTANCE; private static final EnumSingle $VALUES[];
static { INSTANCE = new EnumSingle( "INSTANCE" , 0 ); $VALUES = ( new EnumSingle[] { INSTANCE }); } } |
DCL懒汉式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.wanshi.single;
public class Lazy {
private static Lazy lazy;
public static Lazy getInterface() { synchronized (Lazy. class ) { if (lazy == null ) { lazy = new Lazy(); } } return lazy; }
public static void main(String[] args) { Lazy lazy = Lazy.getInterface(); Lazy lazy2 = Lazy.getInterface(); System.out.println(lazy); System.out.println(lazy2); } } |
双检锁懒汉式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.wanshi.single;
import java.lang.reflect.Constructor; import java.lang.reflect.Field;
public class LazyMan {
private static boolean flag = false ;
private LazyMan() { synchronized ( this ) { if (!flag) { flag = true ; } else { throw new RuntimeException( "不要试图通过反射破坏对象" ); } } }
private volatile static LazyMan lazyMan;
//双重检查锁,懒汉式(DCL懒汉式) public static LazyMan getInstance() { if (lazyMan == null ) { synchronized (LazyMan. class ) { if (lazyMan == null ) { //不是原子性操作,1.分配内存空间,2.执行构造方法,3.把对象指向这个空间 指令重排可能会发生 加上volatile关闭指令重排 lazyMan = new LazyMan(); } } } return lazyMan; }
public static void main(String[] args) throws Exception { // LazyMan lazyMan1 = LazyMan.getInstance();
Field flag = LazyMan. class .getDeclaredField( "flag" ); flag.setAccessible( true );
Constructor<LazyMan> declaredConstructor = LazyMan. class .getDeclaredConstructor( null );
declaredConstructor.setAccessible( true ); LazyMan lazyMan1 = declaredConstructor.newInstance(); flag.set(lazyMan1, false ); LazyMan lazyMan2 = declaredConstructor.newInstance(); System.out.println(lazyMan1); System.out.println(lazyMan2); } } |
为什么要使用 volatile 关键字呢
不是原子性操作
1.分配内存空间
2.执行构造方法
3.把对象指向这个空间
指令重排可能会发生 加上volatile关闭指令重排
内部类懒汉式
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.wanshi.single;
public class Holder {
private Holder() {
}
public static class InnerClass { private static final Holder HOLDER = new Holder(); } } |
案例全部通过测试!
小结
单例模式共有5种创建方式,分别为饿汉式、DCL懒汉式、双检锁懒汉式、Enum枚举饿汉式,内部类懒汉式,这几种方式要掌握,项目中对于全局唯一的对象将其封装为单例模式,开箱即用,非常方便,以及面试中,会让手写单例模式,可谓是大厂必备!
以上就是详解Java单例模式的实现与原理剖析的详细内容,更多关于Java单例模式的资料请关注其它相关文章!
原文链接:https://blog.csdn.net/weixin_45526437/article/details/124702055
查看更多关于详解Java单例模式的实现与原理剖析的详细内容...