本文介绍了android MVP - 我可以有多个演示者用于自定义视图和片段吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个已经与活动相关联的演示者.这本书说一个演示者应该与一个视图相关联.但是现在我添加了一些片段和许多自定义视图.我也在考虑将片段作为一个视图.自定义视图将包含一些逻辑.当然,片段和自定义视图都包含在我的活动中.

So I have an a presenter that is already tied to an activity. The book says that one presenter should be tied to one view. But now I am adding a few fragments and lots of custom views. I am considering a fragment to be a view as well. The custom views will contain a little bit of logic in them. Both the fragments and custom views are contained in my activity of course.

我的问题是,我应该在片段和自定义视图中重复使用相同的演示者,还是每个视图都应该有自己的演示者?我意识到这都是基于意见的,但我想要测试和保持代码清洁的最佳方法.

My question is, should I re-use the same presenter in the fragment and custom views or should each view get its own presenter? I realize this is all opinion based but I want the best approach for testing and keeping code clean.

如果我确实为所有这些少数人设置了一个演示者,那么演示者使用的接口中将包含许多回调方法.同时,如果我做相反的事情并为每个视图创建一个演示者,它可能更容易阅读,但我将如何测试它?

If i do have one presenter for all these fews then then interface the presenter uses will have many callback methods in it. Meanwhile if i did the opposite and created one presenter for each view it might be easier to read but how would i test it ?

推荐答案

View (Activity) 可以有多个 Presenters.如果 Activity 有多个 CustomViews,您可以为每个 设置一个巨大的 PresenterPresenter自定义视图.这取决于:

View (Activity) can have multiple Presenters. In case of having multiple CustomViews for Activity, you can have one giant Presenter or Presenter per each CustomView. It depends on this:

  1. 如果所有 CustomViews 有相同的需求,所有 CustomViews 使用一个 Presenter 就足够了.Presenter's 范围还有两个选项:

  1. If all CustomViews share same needs, one Presenter for all CustomViews is enough. Still two options for Presenter's scope:

  • PresenterActivityScope.Activity 使用 Presenter 并从 Presenter 调用.然后将命令、数据发送到 CustomViews
  • PresenterViewScope.每个 CustomView 创建和销毁相同的 Presenter
  • Presenter has ActivityScope. Activity uses Presenter and gets called from Presenter. Then sends commands, data to CustomViews
  • Presenter has ViewScope. Each CustomView creates and destroys same Presenter

如果CustomViews 的需求不同,有一个PresenterViewInterface,它们将包含所有CustomViews 需要,所以每个 CustomView 都必须实现 ViewInterface 中所有声明的方法,留一些空.

In case of CustomViews not sharing same needs, having one Presenter and ViewInterface, they will contain methods of all CustomViews needs, so each CustomView has to implement all declared methods in ViewInterface, leave some empty.

如果CustomViewsPresenter有不同的需求和方法调用,他们应该有自己的Presenter.

If CustomViews have different needs and method calls to Presenter, they should have their own Presenter.

这篇关于android MVP - 我可以有多个演示者用于自定义视图和片段吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 01:54