@linux1s1s
2015-08-26T15:28:35.000000Z
字数 1671
阅读 3353
Java设计模式
我们先来看看网上比较流行的几种单例模式:
public class Singleton {
private Singleton() {}
private static Singleton single=null;
public static Singleton getInstance() {
if (single == null) {
single = new Singleton();
}
return single;
}
}
public static synchronized Singleton getInstance() {
if (single == null) {
single = new Singleton();
}
return single;
}
public static Singleton getInstance() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
public class Singleton {
private static class LazyHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){
}
public static final Singleton getInstance() {
return LazyHolder.INSTANCE;
}
}
有没有发现这些流行的代码都没有涉及带有参数的单例模式,如果单例需要传入参数,该如何实现,如果实现不能再套用上面的代码,那么问题来了,该如何处理?
我们先假设,直接把参数按照原先的代码样式传过来,如下所示
public static Singleton getInstance(final Object object) {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton(object);
}
}
}
return singleton;
}
这里给出了双边加锁传入参数的例子,单边加锁和嵌套类实现道理相同。虽然代码轻易实现了,但是先别高兴,如果仔细看看代码,你会发现,如果传入不同的参数是不是可能导致单例不再是同一个实例了呢?即使能保证单例,但是语义上已经完全不符合单例的要求,所以该换个更符合实际项目的单例模式,这里仅仅给出双边加锁的线程安全单例代码如下
public class DeviceInfoManager {
private static DeviceInfoManager DEVICE_INFO_MANAGER = null;
private DeviceInfoManager(final Context context) {
//TODO
}
public static void createInstance(final Context context) {
if (DEVICE_INFO_MANAGER == null) {
synchronized (DeviceInfoManager.class) {
if (DEVICE_INFO_MANAGER == null) {
DEVICE_INFO_MANAGER = new DeviceInfoManager(context);
}
}
}
}
public static DeviceInfoManager getInstance() {
if (DEVICE_INFO_MANAGER == null) {
throw new NullPointerException("getInstance() is Null, please call DeviceInfoManager.createInstance(context) first!");
}
return DEVICE_INFO_MANAGER;
}
}