MVP 模式假设 View 向 Presenter 公开方法。例如在 View 中我们可以这样写:
public HasClickhandlers getButton() {
return myBtn;
}
并从演示者访问此方法,例如
view.getButton().addclickhanlder() ...
但是当我以这种风格构建我的应用程序时,我有很多不必要的代码。比如我要创建TablesViewand TablesPresenter(我决定TablesPresenterandTablesView是最小实体(minimal module),不能再分成更小的presenters和view,也不能更复杂)。然后,当我创建时TablesView,我想在这个视图中放入另一个自定义组件 - MyCustomTable。并且 Inside MyCustomTableput MyCustomHeader, inside MyCustomHeaderputMyCustomFilter等等(这个序列可能要长得多)......所以问题是当我想从 Presenter 访问输入的文本(按用户)里面MyCustomFilter时,我需要公开方法MyCustomFilter:
//inside MyCustomFilter
public String getFilterText() {
return textBox.getText();
}
然后在包含的小部件中MyCustomFilter- 即MyCustomHeader我需要公开此方法:
//inside MyCustomHeader
public String getFilterText() {
return myCustomFilter.getFilterText();
}
在它之后,MyCustomTable我需要在里面公开这个方法:
//inside MyCustomTable
public String getFilterText() {
return myCustomHeader.getFilterText();
}
之后,我需要在 内部(包含)公开getFilterText()方法,并且在所有这些操作之后,我的演示者可以访问内部的文本。有时这个序列更长。那么如何解决这个问题呢?可能是我不了解MVP的一些事情吗?TablesViewMyCustomTableMyCustomFilter