获取数据源数据的实现---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团队第五周 - Scrum会议7
Scrum会议7 小组名称:Thunder 项目名称:i阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...
- Java异常(Exception)
Java异常:运行期出现的错误 1. Java异常是Java提供的用于处理程序中错误的一种机制: 2. 错误指的是程序运行期间发生的异常事件,如除零溢出.数组下标越界.读取的文件不存在.... 3. ...
- http和https的异同
转自:http://blog.csdn.net/whatday/article/details/38147103 什么是 HTTPS? HTTPS (基于安全套接字层的超文本传输协议 或者是 HTTP ...
- Spring Boot(三)自动装配
@Configuration和@Bean Spring提供了注解@Configuration和@Bean注解用来配置多个Bean,在以前的Spring项目中可以通过xml的方式配置: <bean ...
- Scrum 冲刺博客,项目总结
1.各个成员在 Alpha 阶段认领的任务 数据库环境的搭建,连接数据库:张陈东芳 数据库语句sql语句:张陈东芳 商品实体类的实现:吴敏烽 获取所有商品信息的实现:吴敏烽 根据商品编号获得商品资料: ...
- c#对xml的操作
操作xml可以通过XElement对象,比较方便的使用列举以下几点: 把字符串转变成XElement,保存成xml文件,加载xml文件: //把字符串解析成XElement对象 string str ...
- RT-thread-2.0.1移植(基于STM32F4xx)
1.将下载的rt-thread-2.0.1解压后,得到如下图所示的文件列表. 在bsp目录下可以找到stm32f40x文件夹,这文件夹里面包括了库函数,其他芯片平台的文件夹统统删掉.在libcpu下, ...
- 【bzoj1038】瞭望塔 半平面交
题目描述 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, y1), ( ...
- jQuery全选反选实例
1. $('#tb:checkbox').each(function(){ 每次都会执行 全选-取消操作,注意$('#tb :checkbox').prop('checked',true); tb后面 ...
- [洛谷P5057][CQOI2006]简单题
题目大意:有一个长度为$n$的$01$串,两个操作: $1\;l\;r:$把区间$[l,r]$翻转($0->1,1->0$) $2\;p:$求第$p$位是什么 题解:维护前缀异或和,树状数 ...