一、饿汉(消耗资源)
private static OkHttpClient okHttpClient = new OkHttpClient();
private okHttpClient(){}
private OkHttpClient getInstance(){
return okHttpClient;
}
二、懒汉
1、普通模式(会有线程安全问题)
private static OkHttpClient okHttpClient;
private okHttpClient(){}
private OkHttpClient getInstance(){
if (okHttpClient == null){
okHttpClient = new OkHttpClient();
}
return okHttpClient;
}
2、同步模式(线程安全,但是会有不必要的开销)
private static OkHttpClient okHttpClient;
private okHttpClient(){}
private synchronized OkHttpClient getInstance(){
if (okHttpClient == null){
okHttpClient = new OkHttpClient();
}
return okHttpClient;
}
三、DCL(线程安全,但是第一次加载反应慢)
private static OkHttpClient okHttpClient;
private okHttpClient(){}
private OkHttpClient getInstance(){
if (okHttpClient == null){
synchronized (OkHttpClient.class){
if (okHttpClient == null){
okHttpClient = new OkHttpClient();
}
}
}
return okHttpClient;
}
四、静态内部类(线程安全,但是延迟了单立的实例化)
private okHttpClient(){}
private OkHttpClient getInstance(){
return SingletonHolder.sInstance;
}
private static class SingletonHolder{
private static final OkHttpClient sInstance = new OkHttpClient();
}
五、枚举(线程安全,简单)
public enum SomeThing {
INSTANCE;
private OkHttpClient instance;
SomeThing(){
instance = new OkHttpClient();
}
public OkHttpClient getInstance(){
return instance;
}
}
六、使用容器
private Map<Integer, Object> map = new HashMap<>();
private okHttpClient(){}
public static void set(){
map.put(uri.hashCode(), listener);
}
public static Object get(String uri){
return map.get(uri.hashCode());
}
借鉴了《Android源码设计模式》
private static OkHttpClient okHttpClient = new OkHttpClient();
private okHttpClient(){}
private OkHttpClient getInstance(){
return okHttpClient;
}
二、懒汉
1、普通模式(会有线程安全问题)
private static OkHttpClient okHttpClient;
private okHttpClient(){}
private OkHttpClient getInstance(){
if (okHttpClient == null){
okHttpClient = new OkHttpClient();
}
return okHttpClient;
}
2、同步模式(线程安全,但是会有不必要的开销)
private static OkHttpClient okHttpClient;
private okHttpClient(){}
private synchronized OkHttpClient getInstance(){
if (okHttpClient == null){
okHttpClient = new OkHttpClient();
}
return okHttpClient;
}
三、DCL(线程安全,但是第一次加载反应慢)
private static OkHttpClient okHttpClient;
private okHttpClient(){}
private OkHttpClient getInstance(){
if (okHttpClient == null){
synchronized (OkHttpClient.class){
if (okHttpClient == null){
okHttpClient = new OkHttpClient();
}
}
}
return okHttpClient;
}
四、静态内部类(线程安全,但是延迟了单立的实例化)
private okHttpClient(){}
private OkHttpClient getInstance(){
return SingletonHolder.sInstance;
}
private static class SingletonHolder{
private static final OkHttpClient sInstance = new OkHttpClient();
}
五、枚举(线程安全,简单)
public enum SomeThing {
INSTANCE;
private OkHttpClient instance;
SomeThing(){
instance = new OkHttpClient();
}
public OkHttpClient getInstance(){
return instance;
}
}
六、使用容器
private Map<Integer, Object> map = new HashMap<>();
private okHttpClient(){}
public static void set(){
map.put(uri.hashCode(), listener);
}
public static Object get(String uri){
return map.get(uri.hashCode());
}
借鉴了《Android源码设计模式》
比较喜好六。(在没有详细研究过单例所有类型的时候自己误撞到了六)