Linux应用程序组件化开发,App 组件化/模块化之路——Repository 模式

什么是 Repository 模式

Repository 这个词直译过来仓库、仓储的意思。这个意思其实也能反应出 Repository 模式作用。App 开发中少不了对数据的操作,数据的来源可能有很多种:网络、数据库、文件以及内存中的缓存。而 Repository 就相当于一个仓库管理员,管理这些数据的存储。当业务层想要获取或者存储数据时,只需要通过 Repository 的管理员进行操作。这样的好处就是:屏蔽数据来源的操作接口。对于业务层来说不必关心数据存在哪里,以及如何存储的。而且也符合我们组件化/模块化架构设计的思想。即当我们更换数据存储设备时,例如从 Android 系统 Sqlite 数据转换为第三方的数据库时,不会影响到业务逻辑。

设计模式

首先预览一下 Repository 模式的设计类图(欢迎拍砖)

Repository.PNG

IDataSource 是定义了数据来源接口,是根据具体的业务需要定义。一般来说,有增、删、改、查这几个方法。

LocalRepository 封装的是本地存储方式,实现 IDataSource 接口。

RemoteRepository 封装的是网络存储方式,实现 IDataSource 接口。

其中 LocalRepository 与 RemoteRepository 就是代表着各种存储方式的具体实现。而 RepositoryFactory就是传说中的“仓库管理员”,管理着各种存储方式,它也是业务层与数据层交互的桥梁。

Show me the code

假设目前有个业务是获取远程数据的需求,如果本地有缓存数据则从本地获取,否则从网络中获取。这样的业务逻辑很常见,我们用 Repository

模式进行封装。

首先预览代码总体结构

repository_code_structure.PNG

IDataSource

public interface IDataSource{voidadd(T t);voiddelete(T t);voidupdate(T t); ListqueryAll(); T queryById(intid);}

LocalRepository

public class LocalRepository implements IDataSource{publicLocalRepository() { } @Overridepublic voidadd(Data data) { DBHelper.get().add(data); } @Overridepublic voiddelete(Data data) { DBHelper.get().delete(data); } @Overridepublic voidupdate(Data data) { DBHelper.get().update(data); } @Overridepublic ListqueryAll() {returnDBHelper.get().queryAll(); } @Overridepublic Data queryById(intid) {returnDBHelper.get().queryById(id); }}

RemoteRepository

public class RemoteRepository implements IDataSource{ @Overridepublic voidadd(Data data) { DataApi.get().add(data); } @Overridepublic voiddelete(Data data) { DataApi.get().delete(data); } @Overridepublic voidupdate(Data data) { DataApi.get().update(data); } @Overridepublic ListqueryAll() {returnDataApi.get().queryAll(); } @Overridepublic Data queryById(intid) {returnDataApi.get().queryById(id); }}

RepositoryFactory

public class RepositoryFactory implements IDataSource{private IDataSourcelocal;private IDataSourceremote;private staticRepositoryFactory INSTANCE;/*** 使用Map实现一个内存缓存*/HashMap mCache = new HashMap<>();private RepositoryFactory(@NonNull IDataSource local, @NonNull IDataSourceremote) {this.local =local;this.remote =remote; }public static RepositoryFactory get(@NonNull IDataSource local, @NonNull IDataSourceremote) {if (INSTANCE == null) { INSTANCE= newRepositoryFactory(local, remote); }returnINSTANCE; }public staticRepositoryFactory get() {if (INSTANCE == null) { INSTANCE= new RepositoryFactory(new LocalRepository(), newRemoteRepository()); }returnINSTANCE; }public voiddestory() { INSTANCE= null; } @Overridepublic voidadd(Data data) { local.add(data); remote.add(data); mCache.put(String.valueOf(data.id), data); } @Overridepublic voiddelete(Data data) { local.delete(data); remote.delete(data); mCache.remove(String.valueOf(data.id)); } @Overridepublic voidupdate(Data data) { local.update(data); remote.update(data); mCache.put(String.valueOf(data.id), data); }/***@return */@Overridepublic ListqueryAll() { List list =local.queryAll();if(list.isEmpty()) { list=remote.queryAll(); }returnlist; }/*** 这里使用三级缓存获取一个Data对象 * *@paramid *@return */@Overridepublic Data queryById(intid) { Data data=mCache.get(String.valueOf(id));if (data == null) { data=local.queryById(id); }if (data == null) { data=remote.queryById(id); }if (data != null) { mCache.put(String.valueOf(id), data); }returndata; }}

使用示例

Flowable.fromCallable(new Callable>() { @Overridepublic List call() throwsException { List dataList =RepositoryFactory.get().queryAll();returndataList; } }).observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Consumer>() { @Overridepublic void accept(@NonNull List datas) throwsException { textView.setText("data size:" +datas.size()); } },new Consumer() { @Overridepublic void accept(@NonNull Throwable throwable) throwsException { textView.setText(throwable.getMessage()); } });

这里是直接使用了 RxJava2 进行调用,因为 Repository 是对数据的请求和访问,这个是耗时操作,故需要放在后台线程中进行。在实际的项目中一般都会使用 MVP 来封装这一层。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值