How dumb can MVP Views really be?

送分小仙女□ 提交于 2019-12-05 18:04:39

The View component must contain sufficient logic to display the interface to the user. Depending upon the framework used, there can be quite a bit of code in the View. The important thing is to ensure business logic lies in the Presenter.

Regarding your secondary query, all Presenter methods will be invoked on the EDT when the View calls them (in the case of Swing). Unless the action required by the Presenter is trivial, I would immediately kick off a background thread to complete the work. That thread will update the View when completed using SwingUtilities.invokeLater().

In fact, to avoid being tied to Swing, I tend to pass my own EventDispatcher class to each Presenter. This is an interface with the same methods as SwingUtilities. I can then substitute in a different class if necessary.

Side note: this can make unit-testing the Presenter with JUnit difficult, because the Presenter method (and the unit test) will complete before the background thread does. I tend to construct each Presenter with an Executor that is responsible for running the background threads. Then, in a unit test environment, I pass in a special Executor implementation that immediately executes the run() method on the same thread. This ensures the unit tests are single-threaded. Example:

public class SingleThreadExecutor implements Executor {
  @Override
  public void execute(Runnable command) {
    command.run();
  }
}

The point of the different flavors of the MVP pattern is to decouple business logic from view logic. Considering that, it's totally fine for having a view which contains a lot of display logic. There are some caveats there though:

Do not use your widgets as data store. The purpose of widgets is to present stuff or to receive input. The presenter and the model are responsible for holding on to state. If the view is stateful, the presenter should be be aware of it and coordinate it. For example, the window size is the view's concern most of the time. But if it is to be persisted to some user settings file, the presenter has to be involved.

There is no requirement that the view is a single, monolithic class. There just has to be a object implementing the interface that the presenter expects, and someone generating the events. For example, Swing uses components like TableModel where the code for specifying the data presentation can be put in. Furthermore, JavaFX and Android make it possible to cut down a lot on the boilerplate code to set up the view.

Events have to be processed in the right thread of course. You could use an event bus which can be told to do the right thing, or pass an ExecutorService into each view. Also, each method to be called by the presenter must offload work to the framework's preferred thread. Better keep that concern out of the presenter, else it will become difficult to test it. The Right Thing To Do depends on the framework.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!