获取数据源数据的实现---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分析拉勾网招聘信息(二): 获取数据
要获取什么样的数据? 我们要获取的数据,是指那些公开的,可以轻易地获取地数据.如果你有完整的数据集,肯定是极好的,但一般都很难通过还算正当的方式轻易获取.单就本系列文章要研究的实时招聘信息来讲,能获取 ...
随机推荐
- Java 继承和访问控制
类的继承 Java中使用extends来实现继承 通过继承,子类自动拥有了基类(supercalss)的所有成员. Java只支持单继承,一个子类只允许有一个基类,一个基类可以有多个子类. class ...
- 转 【.NET平台下使用MongoDB入门教程】
目录 一.了解MongoDB 二.MongoDB特点 三.安装及常用命令 3.1 下载安装 3.2 启动服务器 3.3 常用操作 3.4 其他命令 3.5 做成windows服务 四.批处理程序开启M ...
- ZOJ 2110 C - Tempter of the Bone
https://vjudge.net/contest/67836#problem/C The doggie found a bone in an ancient maze, which fascina ...
- 【APS.NET Core】- Razor Page 使用jqgrid实现分页功能
本文将使用jqgrid在Razor Page中实现分页功能. 前台 List.cshtml代码如下: @page @model ListModel @{ Layout = "~/Pages/ ...
- PHP查询网站
1.w3school http://www.w3school.com.cn/php/ 2.PHP官网 http://php.net/manual/en/funcref.php 3.国内的类似w3csh ...
- 第22天:js改变样式效果
一.输出语句 1.alert:弹出警示框(用的非常少,用户体验不好)完整写法:window.alert(“执行语句”):window对象,窗口,一般情况可省略alert(123); 2.console ...
- java从远程服务器获取PDF文件并后台打印(使用pdfFox)
一.java原生方式打印PDF文件 正反面都打印,还未研究出只打印单面的方法,待解决 public static void printFile(String path) throws Exceptio ...
- bzoj3676-回文串
给出一个字符串,一个子串的出现值为字串出现次数乘以长度,求所有回文串中最大的出现值. 分析 回文自动机模版题,建出自动机后直接统计即可. 回文自动机 类似于后缀自动机,不过一条边\((u,v,c)\) ...
- 【bzoj1705】[Usaco2007 Nov]Telephone Wire 架设电话线 dp
题目描述 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= N < ...
- ssm框架pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...