实际上,虽然演示者 impl [1] 现在需要视图(界面),但演示者界面通常不需要。
典型的模式是:
interface FooPresenter {
// methods called by the view (generally in response to user interaction)
void doSomething(String foo);
}
interface FooView {
/** Tells the view which presenter instance to call back. */
void setPresenter(FooPresenter presenter);
// methods called by the presenter to control the view
void showSomething(String foo);
}
class FooPresenterImpl implements FooPresenter {
private final FooView view;
FooPresenterImpl(FooView view, /* other dependencies here */) {
this.view = view;
}
// could also be the start() method of com.google.gwt.activity.shared.Activity
public void init() {
view.setPresenter(this);
}
// optional; could be called from onStop() and onCancel() if using Activity
public void dispose() {
view.setPresenter(null);
}
}
实际上,我通常将 Presenter 接口声明为嵌套在视图接口中:
interface FooView extends IsWidget {
interface Presenter {
// ...
}
void setPresenter(Presenter presenter);
// ...
}
class FooPresenter extends AbstractActivity implements FooView.Presenter {
// ...
}
Wrt impls 知道 impls,最重要的是演示者 impl 不引用视图 impl,因为它(通常)会阻止在没有GWTTestCase
(模拟视图)的情况下对其进行单元测试。反过来问题不大,但是您并不真的需要演示者界面与 impl,对吗?
[1] 从技术上讲,它可能是视图 impl,但通常是相反的,因此视图可以比演示者更长寿(演示者通常是轻量级的,与视图相反,由于 DOM 操作,视图可能具有不可忽略的创建时间)