Android MVP模式的学习

概要

这个示例是众多的变体。它展示了一个简单的实现Model-View-Presenter模式与体系结构框架。它使用人工依赖注入为存储库提供本地和远程数据源。异步任务处理回调。

Android MVP模式的学习_第1张图片

Note: in a MVP context, the term “view” is overloaded:

The class 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".

Fragments

It uses fragments for two reasons:

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.

Key concepts

There are four features in the app:

Tasks 负责: view 和 presenter 之间定义一个契约(自定义 View 也放在task下)
TaskDetail 负责:一个 Activity 负责创建fragment和presenters
AddEditTask 负责: Fragment 实现 View 的接口

官方实现的示例

Android MVP模式的学习_第2张图片

Each feature has:

1.A contract defining the view and the presenter

view 和 presenter 之间定义一个契约

2.An Activity which is responsible for the creation of fragments and presenters

一个 Activity 负责创建fragment和presenters

3.A Fragment which implements the view interface.

Fragment 实现 View 的接口

4.A presenter which implements the presenter interface

 Presenter 实现 presenter 的接口

In general, the business logic lives in the presenter and relies on the view to do the Android UI work.

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.

Contracts are interfaces used to define the connection between views and presenters.

Dependencies

Common Android support libraries (com.android.support.*)
Android Testing Support Library (Espresso, AndroidJUnitRunner…)
Mockito
Guava (null checking)

你可能感兴趣的:(日记)