单例模式
欢迎来到我的 ChatGPT 中转站,极具性价比,为付费不方便的朋友提供便利,有需求的可以添加左侧 QQ 二维码,另外,邀请新用户能获取余额哦!最后说一句,那啥:请自觉遵守《生成式人工智能服务管理暂行办法》。
# 简介
单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目标是确保某一个类只有一个实例存在。当你希望在任何地方都能访问某个实例时,可以考虑使用单例模式。
在计算机系统中,系统的打印机、文件系统、窗口管理等,通常都是使用单例模式进行设计的。例如,一个系统中可以存在多个打印任务,但是只能有一个正在工作的任务;一个系统中只能有一个文件系统;一个系统中只能有一个窗口管理器等。这些类的对象就适合于用单例模式来设计。
其定义如下:单例模式,确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。
单例模式有八种方式:
- 饿汉式(静态常量)
- 饿汉式(静态代码块)
- 懒汉式(线程不安全)
- 懒汉式(线程安全,同步方法)
- 懒汉式(线程安全,同步代码块)
- 双重检查
- 静态内部类
- 枚举
单例模式都要遵循以下原则
- 构造器私有化:这是为了防止其他类可以通过 new 关键字来创建该类的实例。
- 类的内部创建对象:在类的内部创建一个私有静态的当前类对象。
- 向外暴露一个静态的公共方法:这个公共方法是供外部调用的,用于获取在类的内部创建的那个对象的实例。
# 饿汉式(静态常量)
# 代码实例
package singleton.type1;
/**
* 饿汉式(静态常量)
*/
public class Singleton {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
}
// 类的内部创建静态对象
private static final Singleton instance = new Singleton();
// 构造器私有化
private Singleton() {
}
// 向外暴露一个静态公共方法
public static Singleton getInstance() {
return 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 优缺点说明
- 优点
- 这种写法比较简单,就是在类装载的时候就完成实例化。
- 避免了线程同步问题。
- 缺点
- 在类装载的时候就完成实例化,没有达到 Lazy Loading 的效果。如果从始 至终从未使用过这个实例,则会造成内存的浪费
- 这种方式基于 classloder 机制避免了多线程的同步问题,不过,instance 在类装载时就实例化,在单例模式中大多数都是调用 getInstance 方法, 但是导致类装载的原因有很多种,因此不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化 instance 就没有达到 lazy loading 的效果
- 结论:这种单例模式可用,可能造成内存浪费
# 饿汉式(静态代码块)
# 代码实例
package singleton.type2;
/**
* 饿汉式(静态代码块)
*/
public class Singleton {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
}
private static final Singleton instance;
static {
instance = new Singleton();
}
private Singleton() {
}
// 向外暴露一个静态公共方法
public static Singleton getInstance() {
return 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
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
# 优缺点说明
这种方式的优缺点和上面一致,只不过将类实例化的过程放在了静态代码块中,也是在类装载的时候,就执行静态代码块中的代码,初始化类的实例。
结论:这种单例模式可用,但是可能造成内存浪费
# 懒汉式(线程不安全)
# 代码实例
package singleton.type3;
/**
* 懒汉式(线程不安全)
* 当使用到该方法时才创建,起到了Lazy Loading的效果,但是只能在单线程下使用。
* 如果在多线程下,一个线程进入了if (singleton == null)判断语句块,还未来得及往下执行,另一个线程也通过了这个判断语句,
* 这时便会产生多个实例。所以在多线程环境下不可使用这种方式
*/
public class Singleton {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
}
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (null == instance) {
instance = new Singleton();
}
return 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
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
# 优缺点说明
- 优点
- 起到了 Lazy Loading 的效果,但是只能在单线程下使用。
- 缺点
- 如果在多线程下,一个线程进入了 if (singleton == null) 判断语句块,还未来得及往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例。所以在多线程环境下不可使用这种方式
- 结论:在实际开发中,不要使用这种方式
# 懒汉式(线程安全,同步方法)
# 代码实例
package singleton.type4;
/**
* 懒汉式(线程安全),同步方法方式
*/
public class Singleton {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
}
private static Singleton instance;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if (null == instance) {
instance = new Singleton();
}
return 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
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
# 优缺点说明
- 优点
- 解决了线程不安全问题
- 缺点
- 效率太低了,每个线程在想获得类的实例时候,执行 getInstance () 方法都要进行同步。而其实这个方法只执行一次实例化代码就够了,后面的想获得该类实例, 直接 return 就行了。方法进行同步效率太低
- 结论:在实际开发中,不推荐使用这种方式
# 懒汉式(线程安全,同步代码块)
# 代码实例
package singleton.type5;
/**
* 懒汉式(线程安全),同步代码块方式
*/
public class Singleton {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
}
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (null == instance) {
synchronized (Singleton.class) {
instance = new Singleton();
}
}
return 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
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
# 优缺点说明
- 优点
- 这种方式,本意是想对第四种实现方式的改进,因为前面同步方法效率太低,改为同步产生实例化的的代码块
- 缺点
- 但是这种同步并不能起到线程同步的作用。跟第 3 种实现方式遇到的情形一致,假如一个线程进入了 if (singleton == null) 判断语句块,还未来得及往下执行, 另一个线程也通过了这个判断语句,这时便会产生多个实例
- 结论:在实际开发中,不能使用这种方式
# 双重检查
# 代码实例
package singleton.type6;
/**
* 双重检查
* 这样,实例化代码只用执行一次,后面再次访问时,判断if (singleton == null),直接return实例化对象,也避免的反复进行方法同步.
* 线程安全;延迟加载;效率较高
* 结论:在实际开发中,推荐使用这种单例设计模式
*/
public class Singleton {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
}
// 保证可见性,一个线程修改后,其他线程立刻拿到最新值
private static volatile Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (null == instance) {
// 如果如果有线程已经完成对象创建了,及时进来了也会直接返回
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return 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
34
35
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
# 优缺点说明
- 优点
- Double-Check 概念是多线程开发中常使用到的,如代码中所示,我们进行了两次 if (singleton == null) 检查,这样就可以保证线程安全了。
- 这样,实例化代码只用执行一次,后面再次访问时,判断 if (singleton == null), 直接 return 实例化对象,也避免的反复进行方法同步
- 线程安全;延迟加载;效率较高
- 结论:在实际开发中,推荐使用这种单例设计模式
# 静态内部类
# 代码实例
package singleton.type7;
/**
* 静态内部类
* 这种方式采用了类装载的机制来保证初始化实例时只有一个线程。
* 静态内部类方式在Singleton类被装载时并不会立即实例化,而是在需要实例化时,调用getInstance方法,才会装载SingletonInstance类,从而完成Singleton的实例化。
* 类的静态属性只会在第一次加载类的时候初始化,所以在这里,JVM帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。
* 优点:避免了线程不安全,利用静态内部类特点实现延迟加载,效率高
*/
public class Singleton {
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance1 == instance2);
}
private Singleton() {
}
private static class SingletonInstance {
private static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonInstance.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
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
# 优缺点说明
- 优点
- 这种方式采用了类装载的机制来保证初始化实例时只有一个线程
- 静态内部类方式在 Singleton 类被装载时并不会立即实例化,而是在需要实例化 时,调用 getInstance 方法,才会装载 SingletonInstance 类,从而完成 Singleton 的 实例化。
- 类的静态属性只会在第一次加载类的时候初始化,所以在这里,JVM 帮助我们 保证了线程的安全性,在类进行初始化时,别的线程是无法进入的
- 避免了线程不安全,利用静态内部类特点实现延迟加载,效率高
- 结论:推荐使用
# 枚举
# 代码实例
package singleton.type8;
/**
* 枚举的方式
* 这借助JDK1.5中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。
* 这种方式是Effective Java作者Josh Bloch 提倡的方式
*/
public class Singleton {
public static void main(String[] args) {
SingletonClass instance = SingletonClass.INSTANCE;
SingletonClass instance2 = SingletonClass.INSTANCE;
System.out.println(instance == instance2);
instance.method();
}
}
enum SingletonClass {
INSTANCE;
public void method() {
System.out.println("你好");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 优缺点说明
- 优点
- 这借助 JDK1.5 中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象
- 这种方式是 Effective Java 作者 Josh Bloch 提倡的方式
- 结论:推荐使用
# 单例模式使用的场景
需要频繁的进行创建和销毁的对象、创建对象时耗时过多或 耗费资源过多 (即:重量级对象),但又经常用到的对象、工具类对象、频繁访问数据库或文件的对象 (比如数据源、session 工厂等)
上次更新: 2025/04/12, 07:54:33