基于github androidmvp代码 对mvp进行思考

基于github androidmvp代码 对mvp进行思考

是不是有很多人看了好多mvp的文章,其原理已经了解,但是就是写不出mvp格式的代码或者写出的代码完全是按照别人的模版照搬下来的。照搬下来没问题,不过如果想要简单修改一下模版,就无从下手?本篇文章只是针对刚接触mvp以及资历尚浅的程序猿们。
如果阅读英语没问题的猿友们,可以直接拜读这篇文章以及下面的评论,然后会发现对mvp的编写上茅塞顿开。

参考文章:MVP for Android: how to organize the presentation layer:https://antonioleiva.com/mvp-android/

1、为什么使用mvp模式?

回答:The MVP pattern allows separating(ˈsepəreɪt 使分离) the presentation layer from the logic.
For an application to be easily extensible( [ek’stensɪbl]可扩张的) and maintainable([mein’teinəbl]可维修的),we need to define well-separated layer.
Separating interface from logic in Android is not easy,but the MVP pattern makes it easier to prevent our activities end up degrading into very coupled classes consisting of thundreds or even thouds of lines.

2、为什么借鉴androidmvp的代码?

回答:github上5344个stars,没有issuss;代码写的简洁清晰;又一个基于kotlin版本,支持java8的一些新特性,例如,lambda表达式。
这里写图片描述
这里写图片描述

3、什么是mvp模式?

回答:解藕activity的一种模式,将一个功能划分成Model层(负责准备数据)、V层(负责显示数据和与用户交互)、P层(连接M层和V层)

4、使用在什么场景?

回答:One of the most popular patterns( [‘pætənz]模式) to organize the presentation(展示) layer in Android Application.
MVP is not a architecture by itself,it’s only responsible for the presentation layer.

5、如何编写mvp模式的代码, model view presenter各自的职责是什么?也就是不同层里面需要写入那些代码?

回答:With MVP we take most of logic out from the activities so that we can test it
There are many variations of MVP and everyone can adjust the pattern to their need and the way they feel more comfortable.
I will show how I usually work, but I want this article to be more a place for discussion rather than strict guidelines on how to apply MVP, because up to there is no “standard” way to implement it.
Model:only be the gateway to the domain layer or business logic.it is enough to see as the provider of the data we want to display in the view.
这里写图片描述
View:usually implemented by an Activity(it may be a Fragment,a View……dependingon how the app os structured),will contain a reference to the presenter.
The only thing that the view will do is calling a presenter method every time there is a user action(a button click for example)
这里写图片描述
Then, the activity can implement those methods .
这里写图片描述
The view uses the presenter to notify about user interactions.
这里写图片描述
Presenter:The presenter is responsible to act as the middleman between view and model.
It retrieves data from the model and returns it firmatted to the view.
这里写图片描述

6、MVP的局限?

回答:MVP only models the presentation layer,but the rest of layers will still require a good architecture if you want a flexible and scalable App.

An example of a complete architecture could be Clean Architecture ghough there are many other options.

MVP has some risks, and the most important we use to forget is that the presenter is attached to the view forever. And the view is an activity, which means that:
● We can try to update activities that have already died
Imagine you send a request to a server that takes 10 seconds, but the user closes the activity after 5 seconds. By the time the callback is called and the UI is updated, it will crash because the activity is finishing.
To solve this ,we call the onDestory() method that cleans the view:
这里写图片描述

参考项目:https://github.com/antoniolg/androidmvp

最后提供一些有关模式的官方,个人建议直接阅读官网的说明,比阅读别人的博客要好的多,因为别人写博客的时候加入了自己的理解,必然跟官网的解释有所偏差。
如果英语阅读方面不足,那更得多读,慢慢会发现对程序猿以后的路有很大帮助。

MVVM with architecture components:https://antonioleiva.com/architecture-components-kotlin/
Clean Architecture:https://fernandocejas.com/2015/07/18/architecting-android-the-evolution/
Uncle Bob’s clean architecture:https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
Dagger:http://square.github.io/dagger/
Kotlin-for-Android-Developers:https://github.com/antoniolg/Kotlin-for-Android-Developers
Kotlin online course:https://antonioleiva.com/online-course/

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值