2

我正在编写一个 MVP 风格的 Java Swing 应用程序。该模型适用于与该问题无关的不同线程中的各种计算。当这些更新发生时,我调用EventQueue.invokeLater更新 GUI。这些线程切换调用应该发生在 Presenter 中还是 View 中?

Presenter 的参数:

  • 视图应该尽可能地愚蠢
  • 如果视图这样做了,理论上视图可以EventQueue.invokeLater从 EDT 调用,这是浪费的工作

观点的论据:

  • 无需为ExecutorPresenter 进行单元测试而注入依赖项
  • 视图负责呈现自己,确保在 EDT 上发生更新是该责任的一部分

我不确定。最佳做法是什么?

4

1 回答 1

0

My understanding of MVP is that you would want your Presenter to talk with your View through a separate interface. See this question for a good high-level explanation. That interface is where your calls to invokeLater should go. This will make unit tests easier to write.

Also, just FYI, you can always call out to EventQueue.isDispatchThread() to check before you make any calls to invokeLater or invokeAndWait. InvokeLater won't hurt anything to call it from the EDT, but invokeAndWait would throw an exception.

于 2014-02-09T01:19:21.023 回答