在开发中,需要有一个数据缓存的功能,比如一个表单需要填写用户名、身份证信息、年龄等
直接用sp进行缓存
问题:毫无设计可言,代码写到一堆,无任何扩展
注意:SharedPreferences的原理,也是操作文件,使用xml格式保存,注意commit提交
写到一个工具类里 PreferencesUtils,然后在Application中初始化
PreferencesUtils.getInstance().init(this);
public class PreferencesUtils {
private volatile static PreferencesUtils mInstance;
private SharedPreferences mPreferences;
private SharedPreferences.Editor mEditor;
private PreferencesUtils(){
}
public void init(Context context){
mPreferences = context.getApplicationContext().getSharedPreferences("cache",Context.MODE_PRIVATE);
mEditor = mPreferences.edit();
}
public static PreferencesUtils getInstance(){
if(mInstance == null){
synchronized (PreferencesUtils.class){
if(mInstance == null){
mInstance = new PreferencesUtils();
}
}
}
return mInstance;
}
public PreferencesUtils saveString(String key,String value){
mEditor.putString(key,value);
return this;
}
public void commit(){
mEditor.commit();
}
public String getString(String key){
return mPreferences.getString(key,"");
}
}
问题:无法做到清理部分缓存,无法实现内存缓存与磁盘缓存或者数据库缓存相结合的方式
使用简单工厂模式
public interface IOHandler {
void save(String key,String value);
String getString(String key);
}
// 内存缓存
public class MemoryIOHandler implements IOHandler {
// 存在运行内存里面 内部原理:LinkedHashMap
private static LruCache mCache = new LruCache<>(10 * 1024 * 1024);
@Override
public void save(String key, String value) {
mCache.put(key, value);
}
@Override
public String getString(String key) {
return (String) mCache.get(key);
}
}
// SP缓存
public class SpIOHandler implements IOHandler{
@Override
public void save(String key, String value) {
PreferencesUtils.getInstance().saveString(key,value);
}
@Override
public String getString(String key) {
return PreferencesUtils.getInstance().getString(key);
}
}
public class IOHandlerFactory {
public enum IOType{
MEMORY,PREFERENCES
}
public static IOHandler createIOHandler(IOType ioType){
switch (ioType){
case MEMORY:
return new MemoryIOHandler();
case PREFERENCES:
return new SpIOHandler();
default:
return null;
}
}
}
问题:如果需要新增一个新的缓存方式,就要添加类型,修改工厂类
使用工厂方法模式,每一个工厂都对应一个产品,使用具体产品由外部调用工厂
一个IOFactory 对应 一个IOHandler
public interface IOFactory {
IOHandler createIOHandler();
}
只生产一个产品,如果需要新增类型,只需要创建一个产品并添加该产品的方法
public class MemoryIOFactory implements IOFactory{
@Override
public IOHandler createIOHandler() {
return new MemoryIOHandler();
}
}
public class SpIOFactory implements IOFactory{
@Override
public IOHandler createIOHandler() {
return new PreferencesIOHandler();
}
}
IOFactory ioFactory = new MemoryIOFactory();
IOHandler ioHandler = ioFactory.createIOHandler();
问题:随着功能的扩展,factory类不断增加,而且基本逻辑一样,代码冗余
抽象工厂模式
public interface IOFactory {
IOHandler createIOHandler(Class extends IOHandler> ioHandlerClass);
}
public class IOHandlerFactory implements IOFactory {
private static volatile IOHandlerFactory mInstance;
private IOHandler mMemoryIOHandler,mSpIOHandler;
private IOHandlerFactory(){
}
public static IOHandlerFactory getInstance(){
if(mInstance == null){
synchronized (IOHandlerFactory.class){
if(mInstance == null){
mInstance = new IOHandlerFactory();
}
}
}
return mInstance;
}
public IOHandler createIOHandler(Class extends IOHandler> ioHandlerClass){
try {
return ioHandlerClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return new PreferencesIOHandler();
}
public IOHandler getMemoryIOHandler(){
if(mMemoryIOHandler == null){
mMemoryIOHandler = createIOHandler(MemoryIOHandler.class);
}
return mMemoryIOHandler;
}
public IOHandler getDiskIOHandler(){
return createIOHandler(DiskIOHandler.class);
}
public IOHandler getSpIOHandler(){
if(mPreferencesIOHandler == null){
mSpIOHandler = createIOHandler(SpIOHandler.class);
}
return mPreferencesIOHandler;
}
public IOHandler getDefaultIOHandler(){
return getMemoryIOHandler();
}
}
IOHandler ioHandler = IOHandlerFactory.getInstance().getDefaultIOHandler();
有共性,后期可能有多样性,需要隐藏复杂的创建细节,把对象的实例化和创建封装起来
比如:
数据库的使用,图片加载库,网络加载库等,后期如果有新的框架,想要替换,可以使用工厂模式创建,用最小的改动替换原有的功能