Android MVP-我可以为自定义视图和片段提供多个演示者吗 [英] android MVP - can I have multiple presenters for custom views and fragments

查看:66
本文介绍了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,则每个CustomView可以有一个巨型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:

  • Presenter具有 ActivityScope . Activity使用Presenter并从Presenter调用.然后将命令和数据发送到CustomViews
  • Presenter具有 ViewScope .每个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.

如果CustomViews有不同的需求和对Presenter的方法调用,则它们应具有自己的Presenter.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆