获取数据源数据的实现---Architecting Android
UserRepository,这个接口,描述了Repository提供给用户的功能就是getUsers,getUser(ID)。用户只管使用,其它细节无需理会。
/**
* Interface that represents a Repository for getting {@link User} related data.
*/
public interface UserRepository {
/**
* Get an {@link rx.Observable} which will emit a List of {@link User}.
*/
Observable<List<User>> getUsers(); /**
* Get an {@link rx.Observable} which will emit a {@link User}.
*
* @param userId The user id used to retrieve user data.
*/
Observable<User> getUser(final int userId);
}
UserRepository的一个实现类。通过委托的方式,通过委托userDataStoreFactory,来实现数据获取的功能。
/**
* {@link UserRepository} for retrieving user data.
*/
@Singleton
public class UserDataRepository implements UserRepository { private final UserDataStoreFactory userDataStoreFactory ;
private final UserEntityDataMapper userEntityDataMapper ; private final Func1<List<UserEntity> , List<User>> userListEntityMapper =
new Func1<List<UserEntity> , List<User>>() {
@Override public List<User> call (List<UserEntity> userEntities) {
return UserDataRepository. this.userEntityDataMapper .transform(userEntities) ;
}
}; private final Func1<UserEntity , User>
userDetailsEntityMapper = new Func1<UserEntity , User>() {
@Override public User call (UserEntity userEntity) {
return UserDataRepository. this.userEntityDataMapper .transform(userEntity) ;
}
}; /**
* Constructs a {@link UserRepository}.
*
* @param dataStoreFactory A factory to construct different data source implementations.
* @param userEntityDataMapper {@link UserEntityDataMapper}.
*/
@Inject
public UserDataRepository(UserDataStoreFactory dataStoreFactory ,
UserEntityDataMapper userEntityDataMapper) {
this .userDataStoreFactory = dataStoreFactory;
this. userEntityDataMapper = userEntityDataMapper ;
} @Override public Observable<List<User>> getUsers() {
//we always get all users from the cloud
final UserDataStore userDataStore = this.userDataStoreFactory .createCloudDataStore() ;
return userDataStore.getUserEntityList().map( userListEntityMapper );
} @Override public Observable<User> getUser( int userId) {
final UserDataStore userDataStore = this.userDataStoreFactory .create(userId);
return userDataStore.getUserEntityDetails(userId).map( userDetailsEntityMapper );
}
}
-------------------------------------------------------------------------------------------------------------------
UserDataStore,UserDataStoreFactory
UserDataStoreFactory,选用不同的UserDataStore来实现获取数据的功能。不同的UserDataStore实现代表不同的数据源。
/**
* Interface that represents a data store from where data is retrieved.
*/
public interface UserDataStore {
/**
* Get an {@link rx.Observable} which will emit a List of {@link UserEntity}.
*/
Observable<List<UserEntity>> getUserEntityList(); /**
* Get an {@link rx.Observable} which will emit a {@link UserEntity} by its id.
*
* @param userId The id to retrieve user data.
*/
Observable<UserEntity> getUserEntityDetails (final int userId) ;
}
UserDataStoreFactory,两个create方法,提供了两个不同的UserDataStore实现
/**
* Factory that creates different implementations of {@link UserDataStore}.
*/
@Singleton
public class UserDataStoreFactory { private final Context context;
private final UserCache userCache; @Inject
public UserDataStoreFactory(Context context , UserCache userCache) {
if (context == null || userCache == null) {
throw new IllegalArgumentException( "Constructor parameters cannot be null!!!") ;
}
this .context = context.getApplicationContext() ;
this. userCache = userCache;
} /**
* Create {@link UserDataStore} from a user id.
*/
public UserDataStore create( int userId) {
UserDataStore userDataStore; if (! this.userCache .isExpired() && this.userCache .isCached(userId)) {
userDataStore = new DiskUserDataStore(this. userCache);
} else {
userDataStore = createCloudDataStore();
} return userDataStore;
} /**
* Create {@link UserDataStore} to retrieve data from the Cloud.
*/
public UserDataStore createCloudDataStore() {
UserEntityJsonMapper userEntityJsonMapper = new UserEntityJsonMapper() ;
RestApi restApi = new RestApiImpl(this .context, userEntityJsonMapper) ; return new CloudUserDataStore(restApi , this.userCache );
}
}
-----------------------------------------------------------------------
数据源实现一览
CloudUserDataStore数据源,是通过网络获取数据的。它只要实现了UserDataStore声明的接口便可以,这样,它就是一个UserDataStore了。
/**
* {@link UserDataStore} implementation based on connections to the api (Cloud).
*/
public class CloudUserDataStore implements UserDataStore { private final RestApi restApi;
private final UserCache userCache; private final Action1<UserEntity> saveToCacheAction = new Action1<UserEntity>() {
@Override public void call(UserEntity userEntity) {
if (userEntity != null) {
CloudUserDataStore.this .userCache.put(userEntity) ;
}
}
}; /**
* Construct a {@link UserDataStore} based on connections to the api (Cloud).
*
* @param restApi The {@link RestApi} implementation to use.
* @param userCache A {@link UserCache} to cache data retrieved from the api.
*/
public CloudUserDataStore(RestApi restApi , UserCache userCache) {
this .restApi = restApi ;
this. userCache = userCache;
} @Override public Observable<List<UserEntity>> getUserEntityList() {
return this .restApi.getUserEntityList() ;
} @Override public Observable<UserEntity> getUserEntityDetails (final int userId) {
return this.restApi.getUserEntityById(userId).doOnNext( saveToCacheAction);
}
}
获取数据源数据的实现---Architecting Android的更多相关文章
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- I.MX6 Android Linux shell MMPF0100 i2c 获取数据
#!/system/bin/busybox ash # # I.MX6 Android Linux shell MMPF0100 i2c 获取数据 # 说明: # 本文主要记录通过shell脚本来获取 ...
- Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)
Fragment与Activity之间的数据交换,通常含有3: 一.Fragment从Activity获取数据(仅本文介绍了一个第一): 两.Activity从Fragment获取数据: 三.Frag ...
- Android 开发 values目录里定义数组、颜色、文本、尺寸xml配置文件并且获取数据 附录Android符号转码表
以下xml都在res/values/文件夹下创建 创建String类型array: /app/src/main/res/values/array.xml <?xml version=" ...
- Android开发:SharedPreferences 存储数据、获取数据
Android开发:SharedPreferences 存储数据.获取数据 email:chentravelling@163.com 开发环境:win7 64位,Android Studio. 关于S ...
- Architecting Android…The clean way?
Architecting Android-The clean way? 原文链接:http://fernandocejas.com/2014/09/03/architecting-android-th ...
- 微软BI 之SSIS 系列 - 使用 SQL Profilling Task (数据探测) 检测数据源数据
开篇介绍 SQL Profilling Task 可能我们很多人都没有在 SSIS 中真正使用过,所以对于这个控件的用法可能也不太了解.那我们换一个讲法,假设我们有这样的一个需求 - 需要对数据库表中 ...
- file_get_contents模仿浏览器头(user_agent)获取数据
本篇文章是对file_get_contents模仿浏览器头(user_agent)获取数据进行了详细的分析介绍,需要的朋友参考下 什么是user agentUser Agent中文名为用户代理 ...
- 使用Spark分析拉勾网招聘信息(二): 获取数据
要获取什么样的数据? 我们要获取的数据,是指那些公开的,可以轻易地获取地数据.如果你有完整的数据集,肯定是极好的,但一般都很难通过还算正当的方式轻易获取.单就本系列文章要研究的实时招聘信息来讲,能获取 ...
随机推荐
- 20145214 《Java程序设计》第1周学习总结
20145214 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 了解了Java的诞生和版本演进的历史,目前的最新版本是Java SE8. java的三大平台分别是Java ...
- Java 类和Static关键字
类的定义 类的命名.首字母大写 大括号后面没有分号 成员变量 Java会自动初始化成员变量但是不会自动初始化局部变量: 可以在定义成员变量是直接初始化,成员变量的作用范围在整个类体 对象的创建和引用的 ...
- unity 学习记录
世界第九条约定 缘起 嗯,其实一开始我知道unity是个弄游戏的,也知道好像神庙逃亡,炉石都是出自unity,然后舍友都报了,我也觉得这个东西挺高大上的,所以忍不住自己的双手,报了名,确实,这能学到很 ...
- 开启假期JAVA之路
. 从最基础的JAVA开始学起,已经上了三节课啦!希望在课程结束后能完成一个令自己满意的连连看项目,期待ing~ 慢慢的从简单的代码上手了~ . 用循环输出等腰三角形的效果 import java.u ...
- LintCode-212.空格替换
空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是"真实的"字符长度. 你的程序还需要返回被替换后的字符串 ...
- PAT 甲级 1032 Sharing
https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...
- Python 时间推进器-->在当前时间的基础上推前n天 | CST时间转化标准日期格式
由于公司任务紧迫,好久没有在园子里写自己的心得了,今天偷个闲发表点简单的代码块,在开源的时代贡献微薄力量.话不多说,直接上代码块: ]) m = ]) d = ]) the_date = dateti ...
- C++基础知识(一)
C++中头文件中class的两个花括号后面要加上分号,否则会出现很多的莫名奇妙的错误. 一. 每一个C++程序(或者由多个源文件组成的C++项目)都必须包含且只有一个main()函数.对于预处理指令, ...
- 【bzoj1441】Min 扩展裴蜀定理
题目描述 给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小 输入 第一行给出数字N,代表有N个数 下面一行给出N个数 输出 S ...
- CentOS 压缩(打包)和解压
1.tar命令 -c 创建压缩文件 -x 解开压缩文件 -t 查看压缩包内有哪些文件 -z 用 Gzip压缩或解压 -j 用 bzip2压缩或解压 -v 显示压缩或解压的过程 -f 目标文件名,在 f ...