介绍
单例模式是应用最广的模式之一,也可能是很多初级工程师唯一会使用的设计模式,在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个全局对象,这样有利于我们协调系统整体的行为。
也就是确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。
实现方式
懒汉模式(线程不安全)
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
这种写法lazy loading很明显,但是致命的是在多线程不能正常工作。
懒汉模式(线程安全)
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
这种方式的优点是单例模式只有在使用时才会被实例化,节约了资源,并且线程安全,每次使用都要同步开销,反应稍慢,性能差。
饿汉模式
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
这种方式基于
classloder
机制避免了多线程的同步问题,不过,
instance
在类装载时就实例化,虽然导致类装载的原因有很多种,在单例模式中大多数都是调用
getInstance
方法,
但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化
instance
显然没有达到
lazy loading
的效果。
饿汉模式(变种)
public class Singleton {
private Singleton instance = null;
static {
instance = new Singleton();
}
private Singleton (){}
public static Singleton getInstance() {
return this.instance;
}
}
表面上看起来差别挺大,其实变种实际没变化,都是在类初始化即实例化instance。
Double Check Lock(DCL)实现单例
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class){
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
DCL的优点:资源利用率高,第一次执行才会被实例化,效率高
缺点:第一次加载时反应慢,因为过多判断和同步锁,而且由于Java内存模型的原因偶尔会失败。
静态内部类
public class Singleton {
private static class SingletonHolder {
private static final Singleton mInstance = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.mInstance;
}
}
第一次调用getInstance方法会导致虚拟机加载SingIetonHolder类,这种方式不仅能够确保线程安全,也能保证单例对象的唯一性,同时也延迟了单例的实例化,节省了资源,
推荐使用静态内部类实现方式。
枚举单例
public enum Singleton {
INSTANCE;
public void whateverMethod() {
}
}
这种方式是Effective Java作者Josh Bloch 提倡的方式,它不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象,可谓是很坚强的壁垒啊,不过,个人认为由于1.5中才加入enum特性,用这种方式写不免让人感觉生疏,在实际工作中,我也很少看见有人这么写过。
使用容器实现
public class SingletonManager {
private static Map<String,Object> objMap = new HashMap<String, Object>();
private SingletonManager(){}
public static void registerService(String key,Object instance){
if(!objMap.containsKey(key)){
objMap.put(key, instance);
}
}
public static Object getService(String key){
return objMap.get(key);
}
}
在程序的初始,将多种单例类注入到一个统一的管理类中,在使用时根据key获取对象对应类型的对象。这种方式使得我们可以管理多种类型的单例,并且在使用时可以通过统一的接口进行获取操作,降低了用户的使用成本,也对用户隐藏了具体实现,降低了耦合度。