【发布时间】:2023-06-07 18:19:01
【问题描述】:
我已经开始了一个 GWT 项目,我决定尝试一下 UiBinder。我很难在 UiBinder 上放置 MVP 模式。
当我使用 GWT-pure-java 时:我会使用 Gin 为我的演示者注入相应的视图。这很简单,如果我想将 id 传递给 Presenter,那么我只需将 id 传递给 Presenter 的构造函数即可。
UiBinder 不是那么直接。我几乎可以肯定我遗漏了一些东西,因为很多人都声称 UiBinder 和 MVP 是天作之合……所以我希望在这个问题上得到一些可靠的回应 ;-)
我在几个简单的 GWT-UiBinder 示例中看到的是,视图是由活页夹创建的,然后:
- 视图在其构造函数中或通过
@UIFactory方法构造演示者。 - 对应的presenter被传递给视图(通过setter,不用说在视图构建之后)。
使用第一种方法,如果在视图中构建演示者,如何将 id 传递给演示者?你会做view.getPresenter().setId(42);,然后演示者会去服务器获取一些信息并要求视图显示它......闻起来很糟糕。
使用第二种方法,最终会得到一个非直观的对象图,其中不清楚谁是消费者,谁是生产者。此外,在视图需要来自演示者的信息的情况下(几乎所有用例都需要这个),我们会做什么:
//some code would create the presenter pass it the id and then call view.setPresenter
class MyView {
void setPresenter(MyPresenter p) {
this.presenter = p;
//OK now that i have my presenter how do I ask it to fetch data from the server.
//do i turn around and do: presenter.setView(this); and then the presenter takes
//over and uses the view to display the data?
}
}
这同样臭...对不起,很长的帖子,提前谢谢...
【问题讨论】: