Java Android MVP:什么是交互器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35746546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:05:04  来源:igfitidea点击:

Android MVP: What is an Interactor?

javaandroiddesign-patternsmvpandroid-project-template

提问by bkach

What is an Interactor? How does it fit within the MVP Design? What are the advantages/disadvantages of using an interactor vs putting the interactor code in the presenter?

什么是交互器?它如何适应 MVP 设计?使用交互器与将交互器代码放在演示者中的优点/缺点是什么?

采纳答案by aldok

MVP exists to tackle God Activity problem (An Activity/Fragment that has way too many lines).

MVP 的存在是为了解决 God Activity 问题(有太多行的 Activity/Fragment)。

While it wasn't obligatory (you can code in any pattern that you want), many developers agree that MVP is suitable for Android. It makes your source code cleaner, testable, maintainable and robust.

虽然它不是强制性的(您可以按照您想要的任何模式进行编码),但许多开发人员同意 MVP 适用于 Android。它使您的源代码更清晰、可测试、可维护和健壮。

You can think of an interactor as your "Model/Controller". An interactor will fetch data from your database, web services, or any other data source. After getting the data, the interactor will send the data to the presenter. Thus, making changes in your UI.

您可以将交互器视为您的“模型/控制器”。交互器将从您的数据库、Web 服务或任何其他数据源获取数据。交互者获取数据后,将数据发送给演示者。因此,在您的 UI 中进行更改。

Advantages of using interactor in a separate class is that it will decouple your class, thus making it cleaner and testable. Sure, you can put the interactor in your presenter inner class, but what's the point? The disadvantages of putting the interactor in your presenter is it will make your presenter class bigger and relatively harder to read and manage.

在单独的类中使用交互器的优点是它将解耦您的类,从而使其更清晰和可测试。当然,您可以将交互器放在演示者内部类中,但有什么意义呢?将交互器放在演示者中的缺点是它会使演示者类更大并且相对更难阅读和管理。

Update: Of course this is just an over-simplification, if you want to dig deeper you may see fernando cejas blogor antonio leiva blog

更新:当然这只是过度简化,如果你想深入挖掘,你可能会看到fernando cejas 的博客antonio leiva 的博客

回答by Farhad Faghihi

Interactor contains the use-cases of the application, which means that it will contain all the implementations for the business domain of the project.

Interactor 包含应用程序的用例,这意味着它将包含项目业务领域的所有实现。

Here is a very well-organized article on Architecturing Android Applications, using the MVP pattern., which I highly recommend you to study.

这是一篇关于使用 MVP 模式构建 Android 应用程序的组织良好的文章,我强烈建议你学习。

Also I have created an android application called JuicyInsta, using the MVP pattern and Instagram API, which is shared here on github.

此外,我还使用 MVP 模式和 Instagram API创建了一个名为JuicyInsta的 android 应用程序,该应用程序在 github 上共享。

回答by br00

Personally I use View, Present and Interactor that for me is different from the model.

我个人使用 View、Present 和 Interactor,这对我来说与模型不同。

You can think about an Interactor as a class with useful methods to retrieve the data from the database, server, etc. After you get the data you can populate your model in the Interactor and give it back to the Presenter.

您可以将Interactor 视为一个具有从数据库、服务器等检索数据的有用方法的类。获得数据后,您可以在交互器中填充模型并将其返回给演示者。

E.G. You can have a LoginInteractor that creates an Asynctask to authenticate the user and then populate the UserModel with the data received.

EG 你可以有一个 LoginInteractor 来创建一个 Asynctask 来验证用户,然后用接收到的数据填充 UserModel。

回答by silwar

Interactor is a class which separates Domain Layer from Presentation Layer. In simple words it provides way to write business logic separately than code which is used for manipulate UI (by binding data to UI/ animate / navigation).

Interactor 是一个将领域层与表示层分开的类。简而言之,它提供了单独编写业务逻辑的方法,而不是用于操作 UI 的代码(通过将数据绑定到 UI/动画/导航)。

So Interactor is mediator between Presenter/ViewModel and Repository pattern.

所以Interactor 是Presenter/ViewModel 和Repository 模式之间的中介。

I haven't used Interactor pattern in MVP, I have used it in MVVM though. Interactor can be interchangeably used for UseCases.

我没有在 MVP 中使用过交互器模式,但我在 MVVM 中使用过它。交互器可以互换用于用例。

For example, lets take use case of fetching categories to show in list (In below example, Presenter represents MVP and ViewModel represents MVVM pattern).

例如,让我们使用获取类别以显示在列表中的用例(在下面的示例中,Presenter 代表 MVP,ViewModel 代表 MVVM 模式)。

  • View (Activity/Fragment) will call Presenter/ViewModel's method to get categoryList.
  • Then Presenter/ViewModel will call interactor's method to get categoryList
  • Interactor will call Repository's (CategoryRepository) method to get categoryList
  • Repository will have logic to decide whether to fetch categories from Web Service (Remote Data Source) or from DB storage (Local Data Source) or from cache (temporary storage - can be variable in Repository class).
  • Repository will return categoryList (fetched from selected data source) to Interactor
  • Interactor will either process on categoryList (some formatting etc) and send it to Presenter/ViewModel. Interactor can directly send list to Presenter/ViewModel if no processing is needed
  • Presenter/ViewModel will call View's method with categoryList as parameter
  • View will show categoryList with or without Animation
  • View(Activity/Fragment)会调用Presenter/ViewModel的方法来获取categoryList。
  • 然后Presenter/ViewModel会调用interactor的方法来获取categoryList
  • Interactor 会调用 Repository 的 (CategoryRepository) 方法来获取 categoryList
  • Repository 将有逻辑来决定是从 Web Service(远程数据源)还是从 DB 存储(本地数据源)或从缓存(临时存储 - 可以在 Repository 类中可变)获取类别。
  • Repository 将 categoryList(从选定的数据源获取)返回给 Interactor
  • Interactor 将处理 categoryList(某些格式等)并将其发送到 Presenter/ViewModel。如果不需要处理,Interactor 可以直接将列表发送到 Presenter/ViewModel
  • Presenter/ViewModel 会以 categoryList 为参数调用 View 的方法
  • 视图将显示带或不带动画的类别列表

Please make note that in this process Interactor can be avoided so instead of using data flow like this Repository->Interactor->Presenter/ViewModel, communication can be happened by Repository->Presenter/ViewModelthis way. Here Presenter/ViewModel will be part of Presentation as well as Domain layer. Like I said above Interactor acts as separator of these two layer.

请注意,在此过程中可以避免交互器,因此可以通过这种方式通过Repository->Presenter/ViewModel进行通信,而不是使用像Repository->Interactor->Presenter/ViewModel这样的数据流。这里 Presenter/ViewModel 将成为 Presentation 和 Domain 层的一部分。就像我上面说的,Interactor 充当这两层的分隔符。

These are some concisely written blogs to explain this concept for reference

这些是一些简洁的博客来解释这个概念以供参考

I hope this will help you in understanding role of Interactor in better way. Happy Coding!!!

我希望这能帮助你更好地理解交互器的作用。编码快乐!!!