avatar

Catalog
设计模式——单例模式

涉及到的知识点

  1. 类加载机制
  2. 字节码知识
  3. 类加在机制
  4. jvm指令重排,java序列化机制
  5. Spring 框架 和JDK源码的应用
定义

保证一个类只有一个实例,并且提供一个全局访问点

场景
  1. 线程池
  2. 数据连接池

实现方法

1.懒汉模式
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class SingletonTest{
public static void main(String[] args){
LazySingleton lazy = LazySingle.getInstance();
}
}
// 使用的时候才开始实例化
//jvm 一个实例
public class LazySingleton{
private static LazySingleton instance;
private static LazySingleton getInstance(){
if(instance==null){
instance = new LazySingleton();
}
return instance;
}
private LazySingleton(){
}
}
Author: kim yhow
Link: http://yoursite.com/2020/08/04/设计模式——单例模式/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信
  • 支付寶
    支付寶