获取数据源数据的实现---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分析拉勾网招聘信息(二): 获取数据
要获取什么样的数据? 我们要获取的数据,是指那些公开的,可以轻易地获取地数据.如果你有完整的数据集,肯定是极好的,但一般都很难通过还算正当的方式轻易获取.单就本系列文章要研究的实时招聘信息来讲,能获取 ...
随机推荐
- Thunder团队——事后诸葛亮会议
小组名称:Thunder 项目名称:爱阅APP 小组成员:王航 李传康 代秋彤 邹双黛 苗威 宋雨 胡佑蓉 杨梓瑞 一.设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型 ...
- Alpha阶段中间产物——Thunder团队
Part One 版本控制 git地址:https://git.coding.net/lick468/iReader.git Part Two 软件功能说明书 相关链接:http://www.cnbl ...
- Python学习之路5 - 函数
函数 定义方式: def func(): "这里面写函数的描述" 这里写代码 return x #如果没有返回值就叫"过程",函数和过程的区别就是有无返回值 实 ...
- Mininet实验 多个数据中心的拓扑网络实现
实验目的 掌握多数据中心网络拓扑的构建 掌握多数据中心数据交换过程 实验原理 主机间发送消息上报给交换机,交换机对收到的报文信息进行分析判断,如果交换机中存在此消息相对应的流表,则交换机直接下发流表, ...
- Java中I/O流之轮换流
Java 中的轮换流: 非常有用,可以把一个字节流转换成字符流. inputStreamReader, outputStreamReader Demo_1: import java.io.*; cla ...
- C#通过SC命令和静态公共类来操作Windows服务
调用的Windows服务应用程序网址:http://www.cnblogs.com/pingming/p/5115304.html 一.引用 二.公共静态类:可以单独放到类库里 using Syste ...
- Java中关于 ArrayList 和 Map 的常用遍历方法 (学习笔记,便于以后查询)
一.学习ArrayList与Map时,关于常用遍历方法的记录如下: 二.附源码如下: package com.study.in.myself; import java.util.ArrayList; ...
- Delphi 模式窗体返回值ModalResult的使用方法及注意事项
1.基础知识简介: ModalResult是指一个模式窗体(form.showmodal)的返回值,一般用于相应窗体上按钮的ModalResult属性: 显示完窗体(关闭)后,会返回此属性预设的值做为 ...
- zoj3209-Treasure Map
给出一个左下角为\((0,0)\),右上角为\((n,m)\)的矩形,再给出\(k\)个在大矩形内的小矩形(可以重合),问是否能用这些小矩形完全覆盖这个大矩形,并且没有重合,如果可以至少需要多少个. ...
- python中深copy,浅copy
版权声明:本文为博主原创文章,未经博主允许不得转载. >>> mylist1 = [1, 2, 3, 4] >>> myl = mylist1 >>&g ...