android clean Architecture

最近 因为项目需要重构,所以在找个合适的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的实体类为 RestApiImpl

  public 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的实例是UserListFragment
  private 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();
  }


最后数据就显示出来啦.










 




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值