一席AndroidAndroid开发Android技术知识

Android-Architecture之todo-mvp

2017-02-28  本文已影响138人  小编

todo-mvp官方地址

UI层

项目主要实现了4部分功能:

具体实现

每个功能都是依照如下方式实现:

A presenter typically hosts business logic associated with a particular feature, and the corresponding view handles the Android UI work. The view contains almost no logic; it converts the presenter's commands to UI actions, and listens for user actions, which are then passed to the presenter.

Presenter通常托管这与特定功能相关的业务逻辑,响应的View层处理Android UI工作。视图层几乎不包含逻辑;视图层把Presenter层中的命令转化为UI操作,而且视图层去监听用户的操作,传递给Presenter层。

架构图

mvp.png

项目中包含了Presenter,Repository和DataSource,实现层与层之间的解耦,同时方便单元测试。

按照Android-CleanArchitecture架构图来说,这里只包含了Presentation Layer和Data Layer两层,并未包含Domain Layer。其依赖规则是只能上层依赖下层。

Data层

App需要的数据都是通过Data层中的Repository模块提供,由它去完成访问网络和数据库等。

使用Repository将业务层与具体的查询逻辑分离,Repository的实现类主要职责就是存储查询数据,一个更简单的解释就是你可以把Repository的实现类当成是一个Java容器(Collections),可以从中获取或存储数据(add/remove/etc),他对数据源进行了抽象。Data层的Repository只需要实现相关接口,并提供相关服务即可。

Respository.png

如上图,Repository接口定义了getUserById、getAllUsers等方法,其实现类对接口做具体实现,而真正的操作可能是通过Dao才能访问数据库,或者通过Okhttp访问网络url获取数据。

回到todo项目中:

欢迎交流!
Clean架构探讨

上一篇 下一篇

猜你喜欢

热点阅读