最近 因为项目需要重构,所以在找个合适的android架构。 看到了这个 clean Architecture。
分层架构是最简单的一种架构,现在很多都在使用这种模式。
clean Architecture 分为三个层级
1.presentation
2.domain
3.data
下面简单分析下代码流程:(从数据请求到显示的过程)
绑定注解
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View fragmentView = inflater.inflate(R.layout.fragment_user_list, container, false);
ButterKnife.bind(this, fragmentView);
<pre name="code" class="html"> setupRecyclerView();
return fragmentView;
}
开始请求数据啦...
@Override public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.userListPresenter.setView(this);
if (savedInstanceState == null) {
this.loadUserList();
}
}
由V调到了P(MVP)
private void loadUserList() {
this.userListPresenter.initialize(); <span style="font-family:SimSun;">
}
继续走
定义回调方法 UserListSubsriber 并且由 presentation跳转到domain层
private void getUserList() {
this.getUserListUseCase.execute(new UserListSubscriber());
}
注解绑定 指定name的实体类
对userRepository复制,并由domain层跳转到data层
@Inject public UserListPresenter(@Named("userList") UseCase getUserListUserCase, UserModelDataMapper userModelDataMapper) { return getUserList; }
@Inject public GetUserList(UserRepository userRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread) { super(threadExecutor, postExecutionThread); this.userRepository = userRepository;
指定userRepository实体类@Provides @Singleton UserRepository provideUserRepository(UserDataRepository userDataRepository) { return userDataRepository; }
实际请求方法@SuppressWarnings("Convert2MethodRef") @Override public Observable<List<User>> users() { //we always get all users from the cloud final UserDataStore userDataStore = this.userDataStoreFactory.createCloudDataStore(); return userDataStore.userEntityList() .map(userEntities -> this.userEntityDataMapper.transform(userEntities)); }
指定RestApi的实体类为 RestApiImplpublic UserDataStore createCloudDataStore() { UserEntityJsonMapper userEntityJsonMapper = new UserEntityJsonMapper(); RestApi restApi = new RestApiImpl(this.context, userEntityJsonMapper); return new CloudUserDataStore(restApi, this.userCache); }
上段代码中UserCache的实体类@Provides @Singleton UserCache provideUserCache(UserCacheImpl userCache) { return userCache; }
Api中的请求方法最后数据就显示出来啦.@Override public Observable<List<UserEntity>> userEntityList() { return this.restApi.userEntityList();
Subscriber 为在persentation层定义的回调回调方法的逻辑如下:@RxLogObservable @Override public Observable<List<UserEntity>> userEntityList() { return Observable.create(subscriber -> { if (isThereInternetConnection()) { try { String responseUserEntities = getUserEntitiesFromApi(); if (responseUserEntities != null) { subscriber.onNext(userEntityJsonMapper.transformUserEntityCollection( responseUserEntities)); subscriber.onCompleted(); } else { subscriber.onError(new NetworkConnectionException()); } } catch (Exception e) { subscriber.onError(new NetworkConnectionException(e.getCause())); } } else { subscriber.onError(new NetworkConnectionException()); } }); }
persentation中的回调方法@Override public void onNext(List<User> users) { UserListPresenter.this.showUsersCollectionInView(users); }
viewListView的实例是UserListFragmentprivate void showUsersCollectionInView(Collection<User> usersCollection) { final Collection<UserModel> userModelsCollection = this.userModelDataMapper.transform(usersCollection); this.viewListView.renderUserList(userModelsCollection); } public void setUsersCollection(Collection<UserModel> usersCollection) { this.validateUsersCollection(usersCollection); this.usersCollection = (List<UserModel>) usersCollection; this.notifyDataSetChanged(); }