简体   繁体   English

使用MVP扩展gwt的“Contacts”(示例项目)AppController

[英]Scaling gwt's “Contacts” (sample project) AppController with MVP

I'm just learning GWT so I'm still trying to sort out all of its quirks and features. 我只是在学习GWT,所以我还在努力解决它的所有怪癖和特征。 I'm reading through the example they give illustrating the MVP pattern, and I pretty much get it, except I'm wondering about one thing. 我正在阅读他们给出的描述MVP模式的例子,我几乎得到它,除了我想知道一件事。

The AppController they use implements the ValueChangeHandler interface and the onValueChange method is triggered when history changes. 他们使用的AppController实现了ValueChangeHandler接口,并且在历史记录更改时触发onValueChange方法。

My problem is with this onValueChange in the AppController (i've included it below for anyone who hasn't seen the sample project). 我的问题在于AppController中的onValueChange(我已将它包括在下面,对于那些没有看过示例项目的人)。 It's doing a string comparison on the history token sent in and instantiating the appropriate presenter to handle the action. 它正在对发送的历史记录进行字符串比较,并实例化相应的演示者以处理该操作。 This is all fine and dandy for the sample app with 3 actions, but how would one scale this to a real app with many more actions? 对于具有3个动作的示例应用程序来说,这一切都很好,但是如何将其扩展为具有更多操作的真实应用程序?

Sticking to this pattern would lead to a pretty large/ugly else if , but I'm still too new to GWT (and java) to infer a better pattern for larger apps. 坚持这种模式会导致一个相当大/丑陋的else if ,但我仍然太新GWT(和java)推断更大的应用程序更好的模式。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

public class AppController implements Presenter, ValueChangeHandler<String> {

  ...

  public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();

    if (token != null) {
      Presenter presenter = null;

      if (token.equals("list")) {
        presenter = new ContactsPresenter(rpcService, eventBus, new ContactsView());
      }
      else if (token.equals("add")) {
        presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
      }
      else if (token.equals("edit")) {
        presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
      }

      if (presenter != null) {
        presenter.go(container);
      }
    }
  } 
}

You raise a valid point with large scale GWT application. 您使用大规模GWT应用程序提出有效点。 I recently worked on 50.000+ line GWT portal app and we are getting buried in events and complex switch/handler patterns. 我最近在50.000+线GWT门户应用程序上工作,我们被埋没在事件和复杂的开关/处理程序模式中。 There is a good blog post available here that describes how terrible this can become and also hints at a solution (see terrible footnote ). 有可用的一个很好的博客文章在这里描述有多么可怕这可以成为,也暗示了一个解决方案(见可怕脚注 )。

However the new GWT2 UIBinder and MVP functionality does simplify things. 然而,新的GWT2 UIBinder和MVP功能确实简化了事情。 In fact the author of the above mentioned blog post has written about the places framework (which is a part of GWT 2.1) here . 事实上,上述博客文章的作者已经写了关于地方框架(这是GWT 2.1的一部分) 在这里

The only event the onValueChange method should receives are the "view changing" one. onValueChange方法应该接收的唯一事件是“视图更改”。 Considering each condition is 1 line, it's never going to be THAT big. 考虑到每个条件是1行,它永远不会那么大。 In the end you'll be fine using that pattern. 最后你可以使用这种模式。

As Lars said though, combining UiBinder with the MVP pattern is easy and will greatly reduce the number of code line and make your code easier to modify. 正如Lars所说,将UiBinder与MVP模式相结合很容易,并且会大大减少代码行数并使代码更容易修改。

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

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