【设计模式】单例设计模式的N中Java实现方法

时间:2023-03-08 19:50:13
【设计模式】单例设计模式的N中Java实现方法

转载请注明出处:http://blog.csdn.net/ns_code/article/details/17359719

特点

单例模式的特点:

1、只能有一个实例;

2、必须自己创建自己的一个实例;

3、必须给所有其他对象提供这一实例。

饿汉式单例模式

也称为预先加载法,实现方式如下:

class Single
{
private Single()(
Syustem.out.println("ok");
)
private static Single instance = new Single(); public static Single getInstance(){
return instance;
}
}

优点:线程安全,调用时反应速度快,在类加载的同时已经创建好了一个静态对象;

缺点:资源利用效率不高,可能该实例并不需要,但也被系统加载了。

懒汉式单例模式

也称为延迟加载法,实现方式如下:

public class LazySingleton {
private static LazySingleton instance;
private LazySingleton() {} public static LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
}
return instance;
}
}

延迟加载法在适用于单线程环境,它不是线程安全的,引入多线程时,就必须通过同步来保护getInstance()方法,否则可能会返回LazySingleton的两个不同实例。比如,一个线程在判断instance为null后,还没来得及创建新的instance,另一个线程此时也判断到instance为null,这样两个线程便会创建两个LazySingleton实例。

可以将getInstance()方法改为同步方法,这样便可以避免上述问题,改进后的单例模式实现如下:

public class LazySingleton {
private static LazySingleton instance;
private LazySingleton() {} public static synchronized LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
}
return instance;
}
}

优点:资源利用率高,不执行getInstance就不会被实例。

缺点:第一次加载时反应不快,多线程使用不必要的同步开销大

这里的缺点主要是:每次调用getInstance()方法时,都要同步,而且很多时候的同步是没必要的,这将会极大地拖垮性能(尤其在需要多次调用getInstance方法的地方)。

DCL单例模式

针对延迟加载法的同步实现所产生的性能低的问题,我们可以采用DCL,即双重检查加锁(Double Check Lock)的方法来避免每次调用getInstance()方法时都同步。实现方式如下:

public class LazySingleton {
private static LazySingleton instance;
private LazySingleton() {} public static LazySingleton getInstance() {
if (instance == null) {
synchronized(LazySingleton.class){
if(instance == null){
instance = new LazySingleton();
}
}
}
return instance;
}
}

优点:资源利用率高,不执行getInstance就不会被实例,多线程下效率高。

缺点:第一次加载时反应不快,由于java 内存模型一些原因偶尔会失败,在高并发环境下也有一定的缺陷,虽然发生概率很小。

static内部类单例模式

该方法是为了解决DCL方法在并发环境中的缺陷而提出的,关于DCL在并发编程中存在的问题可以参考这篇文章:http://blog.csdn.net/ns_code/article/details/17348313的后半部分,其实现方式如下:

class Single
{
private Single()(
Syustem.out.println("ok");
) private static class InstanceHolder{
private static final Singlet instance = new Single();
} public static Single getInstance(){
return InstanceHolder.instance;
}
}

优点:线程安全,资源利用率高,不执行getInstance就不会被实例。

缺点:第一次加载时反应不快。

这里针对最后一种方法补充以下基本知识点:类级内部类(有static修饰的成员内部类)相当于其外部类的成员,只有在第一次使用时才会被装载,而不会在类加载器加载其外部类的时候被装载。因此,资源利用率高。

总结:在Java中由于会涉及到并发编程,考虑到效率、安全性等问题,一般常用饿汉式单例模式或static内部类单例模式,二者优缺点刚好颠倒过来,因此要根据具体情况来使用。

参考资料:http://blog.csdn.net/ns_code/article/details/17348313

http://write.blog.csdn.net/postlist/0/all/draft

http://www.myexception.cn/software-architecture-design/1235112.html

http://m.blog.csdn.net/blog/codezjx/8883599

http://blog.sina.com.cn/s/blog_6d2890600101gb8x.html