SharedPreferences单例模式,支持多对象调用

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.hwariot.lib.tools.ToolsLibAPP;

import java.util.HashMap;

/***
 *@date 创建时间 2018/4/18 15:16
 *@author 作者: W.YuLong
 *@description SharedPreferences的单例模式,支持不同的命名
 */
public class SPSingleton {
    private static volatile HashMap<String, SPSingleton> instanceMap = new HashMap<>();

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    //是否是执行apply的模式,false表示为commit保存数据
    private boolean isApplyMode = false;
    private static final String DEFAULT = "default";

    private SPSingleton(String name) {
        if (DEFAULT.equals(name)) {
            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ToolsLibAPP.get());
        } else {
            sharedPreferences = ToolsLibAPP.get().getSharedPreferences(name, Context.MODE_PRIVATE);
        }
        editor = sharedPreferences.edit();
    }

    public static SPSingleton get(String name) {
        if (instanceMap.get(name) == null) {
            synchronized (SPSingleton.class) {
                if (instanceMap.get(name) == null) {
                    instanceMap.put(name, new SPSingleton(name));
                }
            }
        }
        //这里每次get操作时强制将保存模式改为commit的方式
        instanceMap.get(name).isApplyMode = false;
        return instanceMap.get(name);
    }

    public static SPSingleton get() {
        return get(DEFAULT);
    }

    // 如果用apply模式的话,得要先调用这个方法,
    // 然后链式调用后续的存储方法,最后以commit方法结尾
    public SPSingleton applyMode() {
        isApplyMode = true;
        return this;
    }

    public void commit() {
        isApplyMode = false;
        editor.commit();
    }

    public SPSingleton putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        save();
        return this;
    }

    private void save() {
        if (isApplyMode) {
            editor.apply();
        } else {
            editor.commit();
        }
    }

    public SPSingleton putFloat(String key, float value) {
        editor.putFloat(key, value);
        save();
        return this;
    }

    public float getFloat(String key, float defValue){
        return sharedPreferences.getFloat(key, defValue);
    }

    public SPSingleton putLong(String key, long value) {
        editor.putLong(key, value);
        save();
        return this;
    }

    public long getLong(String key, long defValue){
        return sharedPreferences.getLong(key, defValue);
    }

    public SPSingleton putInt(String key, int value) {
        editor.putInt(key, value);
        save();
        return this;
    }

    public SPSingleton putString(String key, String value) {
        editor.putString(key, value);
        save();
        return this;
    }

    public String getString(String key, String defValue) {
        return sharedPreferences.getString(key, defValue);
    }

    public void removeKey(String key) {
        editor.remove(key);
        save();
    }

    public int getInt(String key, int defValue) {
        return sharedPreferences.getInt(key, defValue);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return sharedPreferences.getBoolean(key, defValue);
    }

}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这个模式是很有意思,而且比较简单,但是我还是要说因为它使用的是如此的广泛, 如此的有人缘,单例就是单一、独苗的意思...
    迎风布阵x阅读 3,638评论 0 0
  • 古诗云:“沉舟侧畔千帆过,病树前头万木春”新事物必将取代旧事物,所以如果故步自封,抱残守缺必定会在巨大的社会竞...
    1路有李阅读 1,315评论 2 7
  • 文 | 桑桑姐 17年因为工作,要长途搬家,作为有娃一族,唯一刚需,当然是买学区房。 日光之下并无新事,美国和中国...
    _飞鱼阅读 5,266评论 9 53
  • 一路走来 听说特别喜欢怀念过去的人总是容易感伤,因为存在在过去里的人和事总会在不经意间挑起你的某根神经,刺激你回忆...
    叼伪徐娘阅读 1,112评论 0 0
  • 晚上吃了山东印象的羊肉汤这次超多羊肉啊……晚上普拉提很给力,基本动作很重要,教练说没有做一个俯卧撑一个月想塑形不可...
    竹林风lf阅读 1,360评论 0 0

友情链接更多精彩内容