How to apply MVP pattern to android project

▼魔方 西西 提交于 2019-12-06 14:36:52

问题


Sorry about my English grammar. My question is little bit stupid but I want to understand clearly about how MVP pattern apply in real application.

I'm developing an android project. I want to apply MVP pattern to my project. I refer to this demo :

https://github.com/antoniolg/androidmvp

I'm very excited with this pattern. But in this demo project, I see each activity (a view) , we always have a presenter and an interactor (model) and something else. So in project which have many screen :

  • How should I manage the presenter and model.

  • With each activity (example LoginActivity), I create a "login" package and put all presenter and model into it. Is it ok ?

  • Can someone give me a small application source code using MVP pattern. (not a demo project).


回答1:


To apply the MVP pattern in android, you need to understand what MVP exactly means when it comes to android. How Model, View and presenter communicates with each other. Here is a diagram to understand it better.

I have applied this pattern to one of my application and it enabled me to unit test my core logic and organise the code better.

You can refer to this link to understand the MVP and the application which I created using MVP.




回答2:


I would say something which is not very helpful but it is up to the project and you :-). Personally I'm having on top level subpackages - presenter, view, model. And we are putting the classes into them. The reason is that in the most cases the model is one for the whole app. And also there are cases (I know they are rare) when a few presenters might share one and the same View.

Kind Regards




回答3:


Few advice and suggestions from my end:

1: Use proper folder structure for your files.

com.app.<appname>.activities
com.app.<appname>.fragments
com.app.<appname>.adapters
com.app.<appname>.models
com.app.<appname>.api
com.app.<appname>.listeners
.
.
.

2: Create app's Application class, Initialise global 3rd party libraries instances in it and create Application class SingtoneInstance to access them.

3: Separation of Views and Business Logic layers for following along with listeners:

APIRequest
DBRequest
ImageCaching
.
.
.

4: Some standardised and common practices : Using proven and stable libraries and Bridge them with your application Business-layer and View-layer. Few libraries like

 API Framework   : Volley
 Image Framework : UniversalImageLoader/Picaso


来源:https://stackoverflow.com/questions/30704349/how-to-apply-mvp-pattern-to-android-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!