简体   繁体   English

Android MVP:什么是交互器?

[英]Android MVP: What is an Interactor?

What is an Interactor?什么是交互器? How does it fit within the MVP Design?它如何适应 MVP 设计? What are the advantages/disadvantages of using an interactor vs putting the interactor code in the presenter?使用交互器与将交互器代码放在演示者中的优点/缺点是什么?

At the time of writing (2016), many projects are written using bad version of MVC pattern.在撰写本文时(2016 年),许多项目都是使用不良版本的 MVC 模式编写的。 Where an Activity/Fragment/Controller has too many line of codes. Activity/Fragment/Controller 的代码行太多。 This issue is commonly called God Activity.这个问题通常被称为上帝活动。 MVP is gaining popularity to solve this issue by decoupling class to a Model, View, and Presenter. MVP 通过将类解耦到模型、视图和展示器来解决这个问题,因此越来越受欢迎。

But MVP itself isn't enough, we also see Interactor and Repository pattern emerges.但是MVP本身是不够的,我们也看到了Interactor和Repository模式的出现。

What is an Interactor?什么是交互器? How does it fit within the MVP Design?它如何适应 MVP 设计?

You can think of an interactor as your "Util" class to Create, Read, Update, and Delete (CRUD) a Model.您可以将交互器视为创建、读取、更新和删除 (CRUD) 模型的“Util”类。 An interactor will fetch data from your database, web services, or any other data source, from a Repository.交互器将从您的数据库、Web 服务或任何其他数据源中的存储库中获取数据。 The Interactor is the "verb" or "action" to get the Model.交互器是获取模型的“动词”或“动作”。

  • GetUser获取用户
  • UpdateProfile更新配置文件
  • DeleteStatus删除状态
  • and so on..等等..

After getting the data, the interactor will send the data to the presenter.交互者获取数据后,将数据发送给演示者。 The presenter decides when or how to use the model to make changes in your UI.演示者决定何时或如何使用模型在您的 UI 中进行更改。

Using an interactor means the business logic is decoupled.使用交互器意味着业务逻辑是解耦的。 Since it's decoupled;因为它是解耦的; the code is reusable, simpler, and testable.代码是可重用的、更简单的和可测试的。

What are the advantages/disadvantages of using an interactor vs putting the interactor code in the presenter?使用交互器与将交互器代码放在演示者中的优点/缺点是什么?

You can put the "interactor code" in the presenter, for example if you're confident the code is simple enough it doesn't need to be extracted to a separate class.您可以将“交互器代码”放在演示器中,例如,如果您确信代码足够简单,则不需要将其提取到单独的类中。 But if you decide to use interactor, the interactor can be reused on other presenters.但是,如果您决定使用交互器,则交互器可以在其他演示者上重复使用。

What about repository?存储库呢?

The repository is the class that responsible for the implementation detail of the CRUD operation, like connecting to a database.存储库是负责 CRUD 操作的实现细节的类,例如连接到数据库。

The Repository contains the implementation detail to get the Model.存储库包含获取模型的实现细节。

class UserRepository {
    fun connectToDb() {}
    fun getUser(): User {}
}

Some people call this Data Source, but I believe the terms are interchangeable.有些人称之为数据源,但我相信这些术语是可以互换的。

Update (2021): Even though the MVP + Interactor is still useful.更新(2021 年):尽管 MVP + Interactor 仍然有用。 MVVM pattern with Android Jetpack is the preferred UI pattern by Google.带有 Android Jetpack 的 MVVM 模式是谷歌首选的 UI 模式。

Interactor is a class which separates Domain Layer from Presentation Layer. Interactor 是一个将领域层与表示层分开的类。 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).简而言之,它提供了单独编写业务逻辑的方法,而不是用于操作 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.我没有在 MVP 中使用过交互器模式,但我在 MVVM 中使用过它。 Interactor can be interchangeably used for UseCases.交互器可以互换用于用例。

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

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/ViewModel this way.请注意,在此过程中可以避免交互器,因此可以通过这种方式通过Repository->Presenter/ViewModel进行通信,而不是使用像Repository->Interactor->Presenter/ViewModel这样的数据流。 Here Presenter/ViewModel will be part of Presentation as well as Domain layer.这里 Presenter/ViewModel 将成为 Presentation 和 Domain 层的一部分。 Like I said above Interactor acts as separator of these two layer.就像我上面说的,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!!!编码快乐!!!

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.这是一篇关于使用 MVP 模式构建 Android 应用程序的组织良好的文章 , which I highly recommend you to study. ,我强烈建议你学习。

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 .您可以将Interactor 视为一个具有从数据库、服务器等检索数据的有用方法的类 After you get the data you can populate your model in the Interactor and give it back to the Presenter.获得数据后,您可以在交互器中填充模型并将其返回给演示者。

EG 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。

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

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