获取数据源数据的实现---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); } }
--------------------------------------------------------------------------------------------------------
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。