Android MVP-我可以为自定义视图和片段提供多个演示者吗 [英] android MVP - can I have multiple presenters for custom views and fragments
问题描述
因此,我有一个已经与活动绑定的演示者.这本书说,一个演讲者应该被限制在一个观点上.但是现在我要添加一些片段和许多自定义视图.我也在考虑将片段作为一种观点.自定义视图将在其中包含一些逻辑.片段和自定义视图都包含在我的活动中.
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
,则每个CustomView
可以有一个巨型Presenter
或Presenter
.取决于此:
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:
-
如果所有
CustomViews
共享相同的需求,则所有CustomViews
一个Presenter
就足够了.Presenter's
范围还有两个选择:
If all
CustomViews
share same needs, onePresenter
for allCustomViews
is enough. Still two options forPresenter's
scope:
-
Presenter
具有 ActivityScope .Activity
使用Presenter
并从Presenter
调用.然后将命令和数据发送到CustomViews
-
Presenter
具有 ViewScope .每个CustomView
都会创建并销毁相同的Presenter
Presenter
has ActivityScope.Activity
usesPresenter
and gets called fromPresenter
. Then sends commands, data toCustomViews
Presenter
has ViewScope. EachCustomView
creates and destroys samePresenter
如果CustomViews
不能满足相同的需求,并且只有一个Presenter
和ViewInterface
,它们将包含所有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.
如果CustomViews
有不同的需求和对Presenter
的方法调用,则它们应具有自己的Presenter
.
If CustomViews
have different needs and method calls to Presenter
, they should have their own Presenter
.
这篇关于Android MVP-我可以为自定义视图和片段提供多个演示者吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!