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);
}
}
--------------------------------------------------------------------------------------------------------
 
我从中可以学习到的内容是:
1.先用接口,声明你将要提供给用户的功能。
 
2.而关于数据获取方面的实现,则是按照如下的方式进行:
实现一个工厂,这个工厂负责选择不同的数据源实例,然后通过方法暴露给调用者。
 
 
比如,UserDataStoreFactory,它提供了两个方法,这两个方法都是用来返回一个UserDataStore实例,至于实例是采用哪种实现的,用户无需关心。采用什么实现,这是在UserDataStoreFactory中控制的。
 
在这个例子中,有如下的UserDataStore实现:CloudUserDataStore, DiskUserDataStore
 
这两个实现,就提供了数据源的具体实现。其实,还可以添加,从数据库获取用户列表,用户详情,可以添加一个DataBaseUserDataStore。总之,可以添加不同的数据源,只要它们实现了UserDataStore接口所声明的功能就可以。
 
 
-------------------------------------------------------------------------------------------
参照上述的实现方式,假如我自己要实现一个登陆功能,那么,我要怎么做?
 
登陆功能不适合上述的实现方式。上述的实现方式,适合于专门用户获取数据的场景。
登陆功能,只是一个REST调用,不是用来获取数据的。

获取数据源数据的实现---Architecting Android的更多相关文章

  1. Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  2. I.MX6 Android Linux shell MMPF0100 i2c 获取数据

    #!/system/bin/busybox ash # # I.MX6 Android Linux shell MMPF0100 i2c 获取数据 # 说明: # 本文主要记录通过shell脚本来获取 ...

  3. Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)

    Fragment与Activity之间的数据交换,通常含有3: 一.Fragment从Activity获取数据(仅本文介绍了一个第一): 两.Activity从Fragment获取数据: 三.Frag ...

  4. Android 开发 values目录里定义数组、颜色、文本、尺寸xml配置文件并且获取数据 附录Android符号转码表

    以下xml都在res/values/文件夹下创建 创建String类型array: /app/src/main/res/values/array.xml <?xml version=" ...

  5. Android开发:SharedPreferences 存储数据、获取数据

    Android开发:SharedPreferences 存储数据.获取数据 email:chentravelling@163.com 开发环境:win7 64位,Android Studio. 关于S ...

  6. Architecting Android…The clean way?

    Architecting Android-The clean way? 原文链接:http://fernandocejas.com/2014/09/03/architecting-android-th ...

  7. 微软BI 之SSIS 系列 - 使用 SQL Profilling Task (数据探测) 检测数据源数据

    开篇介绍 SQL Profilling Task 可能我们很多人都没有在 SSIS 中真正使用过,所以对于这个控件的用法可能也不太了解.那我们换一个讲法,假设我们有这样的一个需求 - 需要对数据库表中 ...

  8. file_get_contents模仿浏览器头(user_agent)获取数据

    本篇文章是对file_get_contents模仿浏览器头(user_agent)获取数据进行了详细的分析介绍,需要的朋友参考下     什么是user agentUser Agent中文名为用户代理 ...

  9. 使用Spark分析拉勾网招聘信息(二): 获取数据

    要获取什么样的数据? 我们要获取的数据,是指那些公开的,可以轻易地获取地数据.如果你有完整的数据集,肯定是极好的,但一般都很难通过还算正当的方式轻易获取.单就本系列文章要研究的实时招聘信息来讲,能获取 ...

随机推荐

  1. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...

  2. Android - 时间 日期相关组件

    源码下载地址 : -- CSDN :  http://download.csdn.net/detail/han1202012/6856737 -- GitHub : https://github.co ...

  3. HttpServletRequest和HttpServletResponse实例

    先看一下web.xml文件配置: <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...

  4. 3ds Max学习日记(一)

      暑假闲来无事学习一发3ds Max.为啥要学这玩意?貌似可以用这东西三维建模.暑期生产实习选了一个搞vr的导师,貌似他忙得很,无奈只好先自己研究一下啦~   vr神马的还是有点意思的,虽然自己仅仅 ...

  5. 网卡多ip 再看arp; arp队列也会缓存skb

    [结论] 当协议失效的时候,skb会挂载arp的neigt的一个链表上,然后直接返回了,相当于数据包发下了,当arp收到数据包去修复neigh的目的地址的时候,会把之前所有的neihe中等待的skb全 ...

  6. Oracle导数据到SQL server的方法总结

    通过oracle10g 访问sql server 2008 导数据步骤 最近在项目中遇到要将Oracle数据库的数据导入到SQL server数据库中,解决办法如下: 一.准备工作 配置Oracle ...

  7. 什么情况下需要检测预装Win8/8.1电脑内置激活密钥(即Win8/8.1 OEM key)?

    1.针对预装在品牌机中的Win8操作系统,因使用了某些软件或自己操作不当导致系统激活Key被替换而激活失效时,你需要找到内置在电脑中的OEM key,重新输入OEM key来激活系统. 2.针对预装W ...

  8. vmware 虚拟机下安装centOS7.0

    当时安装的是 CentOS-7.0-1406-x86_64-DVD.iso 这个版本的镜像,提示: 您已经配置此虚拟机使用64位客户操作系统.但是64位操作系统不可用.此主机具有虚拟化支持能力的,可是 ...

  9. 【swaggerui】swaggerui在asp.net web api core 中的应用

    Swaggerui 可以为我们的webapi提供美观的在线文档,如下图: 实现步骤: NuGet Packages  Install-Package Swashbuckle.AspNetCore 在s ...

  10. 洛谷 P1527 [国家集训队]矩阵乘法 解题报告

    P1527 [国家集训队]矩阵乘法 题目描述 给你一个\(N*N\)的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第\(K\)小数. 输入输出格式 输入格式: 第一行两个数\(N,Q\),表示矩阵大 ...