缩放 gwt 的“联系人” (示例项目)带有 MVP 的 AppController

发布于 2024-08-27 18:29:48 字数 1188 浏览 12 评论 0原文

我刚刚学习 GWT,所以我仍在尝试理清它的所有怪癖和功能。我正在阅读他们提供的说明 MVP 模式的示例,我几乎明白了,但我想知道一件事。

他们使用的AppController实现了ValueChangeHandler接口,并且当历史记录发生变化时会触发onValueChange方法。

我的问题是 AppController 中的 onValueChange (我已将其包含在下面,以供没有看过示例项目的人使用)。它对发送的历史令牌进行字符串比较,并实例化适当的呈现器来处理操作。对于具有 3 个操作的示例应用程序来说,这一切都很好,但是如何将其扩展到具有更多操作的真实应用程序呢?

坚持这种模式会导致相当大/丑陋的else if,但我对 GWT(和 java)还太陌生,无法为大型应用程序推断出更好的模式。

非常感谢任何帮助!

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);
      }
    }
  } 
}

I'm just learning GWT so I'm still trying to sort out all of its quirks and features. 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.

The AppController they use implements the ValueChangeHandler interface and the onValueChange method is triggered when history changes.

My problem is with this onValueChange in the AppController (i've included it below for anyone who hasn't seen the sample project). 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?

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.

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);
      }
    }
  } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-09-03 18:29:48

您通过大规模 GWT 应用程序提出了一个有效的观点。我最近开发了 50.000 多行 GWT 门户应用程序,我们正陷入事件和复杂的开关/处理程序模式中。 这里有一篇很好的博客文章这描述了这会变得多么可怕,并暗示了解决方案(参见可怕的脚注)。

然而,新的 GWT2 UIBinder 和 MVP 功能确实简化了事情。事实上,上述博文的作者已经撰写了有关地点 框架(GWT 2.1 的一部分)这里

You raise a valid point with large scale GWT application. I recently worked on 50.000+ line GWT portal app and we are getting buried in events and complex switch/handler patterns. 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. 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.

我还不会笑 2024-09-03 18:29:48

onValueChange 方法应该接收的唯一事件是“视图更改”事件。考虑到每个条件都是 1 行,它永远不会那么大。最后你会很好地使用该模式。

正如 Lars 所说,将 UiBinder 与 MVP 模式结合起来很容易,并且会大大减少代码行数,并使代码更易于修改。

The only event the onValueChange method should receives are the "view changing" one. Considering each condition is 1 line, it's never going to be THAT big. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文