定义
单例模式(Singleton Pattern)是java最简单的设计模式之一,用于保证某个类在运行期间只有一个实例对外提供服务,我们称这种类型的类为单例类。
设计原则
-
保证一个类只有一个实例;
-
为实例提供一个全局访问点。
实现方式
饿汉式
代码示例
java
package com.sumlv.javase.note;
/**
* 单例示例
*
* @Auther: yuzhuo.song
* @Date: 2025-03-14
*/
public class Singleton {
private static Singleton instance = new Singleton();
public static Singleton getInstance() {
return instance;
}
/**
* 用于避免序列化对单例的破坏
* 如果单例类没有实现序列化接口则不需要该方法
*
* @return 实例对象
*/
private Object readResolve() {
return instance;
}
/**
* 用于避免反射对单例的破坏
*/
private Singleton() {
if (instance != null) {
throw new RuntimeException("illegal access");
}
}
}
补充说明
-
线程安全;
-
不支持延时加载;
-
获取实例对象速度较快;
-
如果实例对象较大且一直未使用会造成内存浪费。
懒汉式
代码示例
java
package com.sumlv.javase.note;
/**
* 单例示例
*
* @Auther: yuzhuo.song
* @Date: 2025-03-14
*/
public class Singleton {
private static Singleton instance;
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
/**
* 用于避免序列化对单例的破坏
* 如果单例类没有实现序列化接口则不需要该方法
*
* @return 实例对象
*/
private Object readResolve() {
return instance;
}
/**
* 用于避免反射对单例的破坏
*/
private Singleton() {
if (instance != null) {
throw new RuntimeException("illegal access");
}
}
}
补充说明
-
支持延迟加载;
-
保证多线程模式下实例的唯一性;
-
并发度低,在频繁调用时会产生性能瓶颈。
双重检查锁
代码示例
java
package com.sumlv.javase.note;
/**
* 单例示例
*
* @Auther: yuzhuo.song
* @Date: 2025-03-14
*/
public class Singleton {
private static volatile Singleton instance;
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
/**
* 用于避免序列化对单例的破坏
* 如果单例类没有实现序列化接口则不需要该方法
*
* @return 实例对象
*/
private Object readResolve() {
return instance;
}
/**
* 用于避免反射对单例的破坏
*/
private Singleton() {
if (instance != null) {
throw new RuntimeException("illegal access");
}
}
}
补充说明
-
支持延迟加载;
-
并发度高,在频繁调用时不会产生性能瓶颈。
静态内部类
代码示例
java
package com.sumlv.javase.note;
/**
* 单例示例
*
* @Auther: yuzhuo.song
* @Date: 2025-03-14
*/
public class Singleton {
public static Singleton getInstance() {
return InnerClass.instance;
}
/**
* 用于避免序列化对单例的破坏
* 如果单例类没有实现序列化接口则不需要该方法
*
* @return 实例对象
*/
private Object readResolve() {
return InnerClass.instance;
}
/**
* 用于避免反射对单例的破坏
*/
private Singleton() {
if (InnerClass.instance != null) {
throw new RuntimeException("illegal access");
}
}
private static class InnerClass {
private static Singleton instance = new Singleton();
}
}
补充说明
-
支持延迟加载;
-
并发度高,在频繁调用时不会产生性能瓶颈。
-
实现方式相较于双重检查锁更为简洁。
枚举
代码示例
java
package com.sumlv.javase.note;
/**
* 单例示例
*
* @Auther: yuzhuo.song
* @Date: 2025-03-14
*/
public enum Singleton {
INSTANCE;
public static Singleton getInstance() {
return INSTANCE;
}
}
补充说明
-
最简单的单例实现方式;
-
线程安全;
-
无需关心反射和序列化对单例造成破坏;