单例模式
定义
单例模式,属于创建型模式,提供了一种对象创建的最佳方式。它确保了一个类只有一个实例,并提供了一个全局访问点。
特征
- 单例类只能有一个实例
- 单例类必须自己创建自己的唯一实例
- 单例类必须给所有其他对象提供这一实例
意图
保证一个类仅有一个实例,并提供一个访问他的全局访问点
主要解决
一个全局使用的类频繁的创建和销毁
何时使用
当你想控制实例数目,节省系统资源的时候
如何解决
判断系统是否有这个实例,有则返回,无则创建
关键代码
私有构造函数
优点
减少了内存开销,避免了实例的频繁创建和销毁
缺点
无接口,不能继承,与单一职责冲突
注意
getInstance() 方法中需要使用同步锁 synchronized (Singleton.class) 防止多线程同时进入造成 instance 被多次实例化。
单例模式的几种实现方式
1. 懒汉式,线程不安全
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
2.懒汉式,线程安全
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
这种加了synchronized,具有很好的懒加载(需要用到创建实例的时候再去创建实例),能够在多线程中很好的工作,但正是由于加了同步,虽然保证了单例,却影响了效率
3. 饿汉式,线程安全
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
static确保了,类在加载的时候,就已经实例了,所以确保了线程安全。缺点是资源效率不高,可能getInstance()永远不会执行到,但执行该类的其他静态方法或者加载了该类(class.forName),那么这个实例仍然初始化,这个时候,就浪费了内存,产生了垃圾对象,达不到lazy loading的效果了
4.双检锁/双重校验锁(DCL,即 double-checked locking )
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getInstance() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
这种方式采用双锁机制,安全且在多线程情况下能保持高性能。
5.登记式/静态内部类
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
6.枚举
public enum SingletonEnum {
/**
* 1.从Java1.5开始支持;
* 2.无偿提供序列化机制;
* 3.绝对防止多次实例化,即使在面对复杂的序列化或者反射攻击的时候;
*/
instance;
private String others;
SingletonEnum() {}
}