Android MVP模式的学习

本文介绍了一种基于Model-View-Presenter(MVP)模式的应用架构实现。通过四个主要组件——契约、Activity、Fragment和Presenter,文章详细阐述了如何利用这些组件进行模块化开发,并解释了它们之间的交互方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概要

这个示例是众多的变体。它展示了一个简单的实现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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值