明白Android中的MVP
我找到了官方讲解MVP的sample(代码示例)
官方示例地址
于是我下载了最基本的一个实现MVP的样例代码.
最基本的TODO-MVP
然后我对他下面的ReadMe做了全部翻译.
TODO-MVP
Summary
This sample is the base for many of the variants. It showcases a simple
implementation of the Model-View-Presenter pattern with no architectural
frameworks. It uses manual dependency injection to provide a repository with
local and remote data sources. Asynchronous tasks are handled with callbacks.
这个样例是那些复杂变体的基础.它展示了一个没有经过任何构建框架装饰过的实现了Model_View_Presenter的样例.它使用手工的依赖注入来提供本地和远程数据源的仓库.异步任务来处理回调.
Diagram
Note: in a MVP context, the term "view" is overloaded:
提示:在一个MVP的上下文里,术语"View"是过载的.
- The android.view.View will be referred to as "Android View"
- The view that receives commands from a presenter in MVP, will be simply called"view".
- 一个类 android.view.View 将被引申为"Android View"(一个称为view的类被叫做安卓视图.)
- 在MVP模式里从presenter(推荐者,主持人,)接收命令的View,被简单的称为view(视图);
Fragments
It uses fragments for two reasons:
使用fragmen有两个原因:
- The separation between Activity and Fragment fits nicely with this
implementation of MVP: the Activity is the overall controller that creates and
connects views and presenters. - Tablet layout or screens with multiple views take advantage of the Fragments
framework. - 将Activity和fragment分离很好的实现了MVP模式:Activity是全面的控制器,用于创建和连接view和presenters.
- Fragments框架有利于有多重组合view的平板式布局视图.
Key concepts 关键概念
There are four features in the app:
这个app有四个功能:
- <code>Tasks事务</code>
- <code>TaskDetail事务详情</code>
- <code>AddEditTask添加编辑事务</code>
- <code>Statistics统计</code>
Each feature has:
每个功能都有:
- A contract defining the view and the presenter
- An Activity which is responsible for the creation of fragments and presenters
- A Fragment which implements the view interface.
- A presenter which implements the presenter interface
- 一个定义view和presenter的规定.
- 一个负责连接fragment和presenter的activity.
- 一个实现了view接口的fragment.
- 一个实现了presenter接口的presenter.
In general, the business logic lives in the presenter and relies on the view to
do the Android UI work. 通常,业务逻辑存在于presenter里面并且依赖于view,它用来完成AndroidUI的工作.
The view contains almost no logic: it converts the presenter's commands to UI
actions and listens to user actions, which are passed to the presenter.
view层几乎不包含任何逻辑:他转换presenter的逻辑给UI的动作,他还监听用户的动作并且传递给presenter.
各种规定都是接口,用来定义views和presenters的连接的.
Contracts are interfaces used to define the connection between views and
presenters.
Dependencies 依赖.
- Common Android support libraries (<code>com.android.support.*)</code>
- 共同的Android支持库
- Android Testing Support Library (Espresso, AndroidJUnitRunner…)
- Android测试的支持库.
- Mockito
- (这是怎么鬼东西?)
- Guava (null checking)
- (这个我也不知道,万能的网友帮忙查一下?)
Features功能
Complexity - understandability
概念的复杂性
Use of architectural frameworks/libraries/tools:
使用的构建, 框架/库/工具
None 没有?
Conceptual complexity
概念的复杂性.
Low, as it's a pure MVP implementation for Android
这是一个为Android的纯MVP实现.
Testability
可测试性
Unit testing
单元测试
High, presenters are unit tested as well as repositories and data sources.
高,presenters都是经过单元测试的,针对存储仓库和数据源.
UI testing
UI 测试
High, injection of fake modules allow for testing with fake data
高注入的假模块允许使用伪造数据测试.
Code metrics
代码矩阵.
Compared to a traditional project with no architecture in place, this sample
introduces additional classes and interfaces: presenters, a repository,
contracts, etc. So lines of code and number of classes are higher in MVP.
相比传统的项目没有构建的地方,
这个示例引入了额外的类和接口:presenters,一个仓库,契约等。所以类的代码的行数和数量在MVP模式里变得更多了。
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Java 46 1075 1451 3451
XML 34 97 337 601
-------------------------------------------------------------------------------
SUM: 80 1172 1788 4052
-------------------------------------------------------------------------------
Maintainability
可维护性
Ease of amending or adding a feature
易于修改或添加功能
High.
高
Learning cost
学习花费
Low. Features are easy to find and the responsibilities are clear. Developers
don't need to be familiar with any external dependency to work on the project.
低.功能都很好找,并且他们的职责都很明确.要这个项目工作,开发者不必熟悉外部的依赖.
2016-11-4 22:20:17
用时:两个番茄