单例设计模式
- 单例设计模式介绍
- 单例设计模式的八种方法
2.1 饿汉式(静态常量)
2.1.1 实现步骤
2.1.2 代码实现
2.1.3 优缺点分析
2.2 饿汉式(静态代码块)
2.1 代码实现
2.2 优缺点分析
2.3 懒汉式(线程不安全)
2.3.1 代码实现
2.3.2 优缺点分析
2.4 懒汉式(线程安全,同步方法)
2.4.1 代码实现
2.4.2 优缺点分析
2.5 略
2.6 双重检查
2.6.1 代码实现
2.6.2 优缺点分析
2.7 静态内部类
2.7.1 代码实现
2.7.2 优缺点分析
2.8 枚举
2.8.1 代码实现
2.8.2 优缺点分析
3 注意事项和细节说明 - 单例设计模式介绍
单例设计模式:就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)。
- 单例设计模式的八种方法
- 饿汉式(静态常量)
- 饿汉式(静态代码块)
- 懒汉式(线程不安全)
- 懒汉式(线程安全,同步方法)
- 懒汉式(线程安全,同步代码块)
- 双重检查
- 静态内部类
- 枚举
2.1 饿汉式(静态常量)
2.1.1 实现步骤
- 构造器私有化(防止 new)
- 类的内部创建对象
- 向外暴露一个静态的公共方法:getInstance()
2.1.2 代码实现
/**
- @ClassName SingletonType01
- @Description 饿汉式 -- 静态变量
- @Author liu wq* @Date 2019/10/13 11:29
- @Version 1.0
/
public class SingletonType01 {
/*- 私有化构造方法
*/
private SingletonType01() {
}
private static final SingletonType01 instance = new SingletonType01();
public static SingletonType01 getGetInstence () {
return instance;
}
}
- 私有化构造方法
2.1.3 优缺点分析
[图片]
2.2 饿汉式(静态代码块)
2.1 代码实现
/**
- @ClassName SingletonType02
- @Description 饿汉式 -- 静态代码块
- @Author liu wq
- @Date 2019/10/13 14:00
- @Version 1.0
/
public class SingletonType02 {
/*- 私有化构造方法
*/
private SingletonType02() {}
private static SingletonType02 instance;
// 静态代码块执行
static {
instance = new SingletonType02();
}
public static SingletonType02 getInstance() {
return instance;
}
}
- 私有化构造方法
2.2 优缺点分析
[图片]
2.3 懒汉式(线程不安全)
2.3.1 代码实现
/**
- @ClassName SingletonType02
- @Description 懒汉式-线程不安全
- @Author liu wq
- @Date 2019/10/13 13:47
- @Version 1.0
*/
public class SingletonType03 {
private SingletonType03() {}
private static SingletonType03 instance;
public static SingletonType03 getInstance() {
if (instance == null) {
instance = new SingletonType03();
}
return instance;
}
}
2.3.2 优缺点分析
[图片]
2.4 懒汉式(线程安全,同步方法)
2.4.1 代码实现
/**
- @ClassName SingletonType04
- @Description 懒汉式 -- 线程安全,同步方法
- @Author liu wq
- @Date 2019/10/13 14:04
- @Version 1.0
*/
public class SingletonType04 {
private SingletonType04() {}
private static SingletonType04 instance;
public static synchronized SingletonType04 getInstance() {
if (instance == null) {
instance = new SingletonType04();
}
return instance;
}
}
2.4.2 优缺点分析
[图片]
2.5 略
2.6 双重检查
2.6.1 代码实现
/**
- @ClassName SingletonType05
- @Description 双重检查方法
- @Author liu wq
- @Date 2019/10/13 14:12
- @Version 1.0
*/
public class SingletonType05 {
private SingletonType05() {}
private static volatile SingletonType05 instance;
public static synchronized SingletonType05 getInstance() {
if (instance == null) {
synchronized (SingletonType05.class) {
if (instance == null) {
instance = new SingletonType05();
}
}
}
return instance;
}
}
2.6.2 优缺点分析
[图片]
2.7 静态内部类
2.7.1 代码实现
/**
- @ClassName SingletonType06
- @Description 静态内部类
- @Author liu wq
- @Date 2019/10/13 14:47
- @Version 1.0
*/
public class SingletonType06 {
private SingletonType06() {}
private static class SingletonInstance {
private static final SingletonType06 INSTANCE = new SingletonType06();
}
public static SingletonType06 getInstance() {
return SingletonInstance.INSTANCE;
}
}
2.7.2 优缺点分析
[图片]
2.8 枚举
2.8.1 代码实现
/**
- @ClassName SingletonType07
- @Description 枚举实现单例
- @Author liu wq
- @Date 2019/10/13 16:04
- @Version 1.0
*/
public enum SingletonType07 {
INSTANCE
}
2.8.2 优缺点分析
[图片]
3 注意事项和细节说明
- 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统性能
- 当想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是使用new
- 单例模式使用的场景:需要频繁的进行创建和销毁的对象、创建对象时耗时过多或耗费资源过多(即:重量级对象),但又经常用到的对象、工具类对象、频繁访问数据库或文件的对象(比如数据源、session工厂等)