您的位置:首页 > 移动开发 > Android开发

Android MVP模式的学习

2016-10-09 17:36 344 查看

概要

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



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 的接口


官方实现的示例



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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android mvp