简体   繁体   English

GWT MVP:最佳设计实践

[英]MVP in GWT : Best design practice

I have read on MVP tutorial of GWT project site that, view should only contain event handlers for widgets it contain and logic for handling them should reside in presenter. 我已经阅读了GWT项目站点的MVP教程,该视图仅应包含其包含的小部件的事件处理程序,并且处理它们的逻辑应驻留在presenter中。 Regarding this i have below doubts : 对此我有以下疑问:

  1. Many times we need to dynamically change the style of widgets on the basis of events received on view, so does it make sense to move this kind of logic in prsenter ? 很多时候,我们需要根据在视图上接收到的事件来动态更改窗口小部件的样式,那么在代理人中移动这种逻辑是否有意义?

  2. Many times we need to get data from several fields in view and create a object and pass it to some widget, like a cellTable, which has its own asyncDataProvider. 很多时候,我们需要从视图中的多个字段获取数据并创建一个对象,然后将其传递给某些小部件,例如cellTable,它具有自己的asyncDataProvider。 So does it make sense in creating getters and setters for all view fields, so that presenter can access them and form object and initialize the cellTable and pass object into it ? 那么在为所有视图字段创建getter和setter时是否有意义,以便演示者可以访问它们并形成对象并初始化cellTable并将对象传递给它? Is it a good idea to add widgets in view's panels in presenter ? 在Presenter的视图面板中添加小部件是个好主意吗?

  3. Everywhere I read that reason for adding logic to presenter instead of view is to increase jUnit test coverage,which saves time. 我到处都读到向演示者添加逻辑而不是视图的原因是为了增加jUnit测试覆盖率,从而节省了时间。 But, as far as I can see, we can use mocking frameworks on View as well to write test cases for basic logic inside them. 但是,据我所知,我们也可以在View上使用模拟框架来为其中的基本逻辑编写测试用例。

  4. Considering point 3, does it really make sense to write so much of code(getters/ setters) in view. 考虑到第3点,在视图中编写这么多的代码(getters / setters)真的有意义吗? I believe that flow should go back to presenter from view, only when : 我认为,只有在以下情况下,流程才应从主持人的角度返回:

    a. 一种。 we need to switch views 我们需要切换视图

    b. we don't have data to be shown in view, so presenter can provide it via RPC 我们没有要显示的数据,因此演示者可以通过RPC提供数据

For starters, please see the Demystifying MVP and EventBus in GWT ( slides ) presentation from Google IO 2013. It's an update to the whole MVP + GWT approach which should answer some of your doubts and questions. 首先,请参阅Google IO 2013 中的GWT中揭秘MVP和EventBus幻灯片 )演示。这是对整个MVP + GWT方法的更新,应该可以回答您的一些疑问和问题。

Generally, there are many opinions and approaches when it comes to MVP and designing the architecture of your application. 通常,有关MVP和设计应用程序体系结构,有很多意见和方法。 So, YMMV. 因此,YMMV。

  1. I'd ask myself - is this a change to the state of data? 我问自己-这是数据状态的变化吗? Or just an update to the presentation? 还是只是演示文稿的更新? In the latter case, I'd keep it in the view, otherwise the view's interface would get unnecessarily bloated. 在后一种情况下,我会将其保留在视图中,否则视图的界面会变得不必要地过大。
  2. In this case, you might want to looking into creating an Editor . 在这种情况下,您可能要考虑创建Editor Then you'd use the editor's interface for editing and flushing the data - you wouldn't have to expose all the separate fields. 然后,您将使用编辑器的界面来编辑和刷新数据-您不必公开所有单独的字段。
  3. That's true - see the presentation above, where it's mentioned that in some cases separating view and presenter doesn't make sense. 是的,请参见上面的演示,其中提到在某些情况下将视图和演示者分开是没有意义的。 Especially when we have tools such as gwtmockito. 特别是当我们拥有gwtmockito之类的工具时。
  4. This depends on how you treat your views - are they dumb views, that don't contain logic and are controlled by presenters? 这取决于您如何对待视图-它们是愚蠢的视图,不包含逻辑并且由演示者控制吗? On one hand, this makes for a nice separation, on the other, as you mentioned, the view's interface tend to get very verbose. 一方面,这实现了很好的分离,另一方面,正如您提到的,视图的界面往往变得非常冗长。 I try to aim for the sweet-spot between this two approaches, but it really depends on your coding style and use cases. 我尝试针对这两种方法之间的最佳结合点,但这实际上取决于您的编码风格和用例。

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

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