简体   繁体   English

如何将MVP模式应用于Android项目

[英]How to apply MVP pattern to android project

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. 我的问题有点愚蠢,但我想清楚地了解MVP模式在实际应用中的应用方式。

I'm developing an android project. 我正在开发一个android项目。 I want to apply MVP pattern to my project. 我想将MVP模式应用于我的项目。 I refer to this demo : 我指的是这个演示:

https://github.com/antoniolg/androidmvp 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. 对于每个活动(例如LoginActivity),我创建一个“登录”包并将所有演示者和模型放入其中。 Is it ok ? 可以吗?

  • Can someone give me a small application source code using MVP pattern. 有人可以使用MVP模式给我一个小的应用程序源代码。 (not a demo project). (不是演示项目)。

To apply the MVP pattern in android, you need to understand what MVP exactly means when it comes to android. 要在Android中应用MVP模式,您需要了解MVP在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. 您可以参考此链接来了解MVP和我使用MVP创建的应用程序。

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 亲切的问候

Few advice and suggestions from my end: 我的建议和建议很少:

1: Use proper folder structure for your files. 1:对文件使用正确的文件夹结构。

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. 2:创建应用程序的Application类,在其中初始化全局第3方库实例,并创建Application类SingtoneInstance进行访问。

3: Separation of Views and Business Logic layers for following along with listeners: 3:将视图和业务逻辑层分离,以便与侦听器一起使用:

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. 4:一些标准化和通用的做法:使用经过验证的稳定库并将它们与应用程序业务层和视图层桥接。 Few libraries like 很少图书馆喜欢

 API Framework   : Volley
 Image Framework : UniversalImageLoader/Picaso

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM