我學習GWT和我在多個地方,使用MVP架構最適合發展GWT應用程序GWT MVP架構優勢
我也看過,它很容易做到使用MVP ARCH.Can測試已經閱讀有人向我解釋爲什麼使用MVP架構輕鬆進行測試。
另外我正在使用MVP的項目,我發現它非常繁瑣,使視圖連接到數據base.I的意思是我必須更新我的演示者,服務,serviceAsync,servicImpl,Facades,以便建立連接到數據庫。因此,有人可以爲我提供GWT的MVP的本質嗎?我會欣賞幾個例子。演示者之間
我學習GWT和我在多個地方,使用MVP架構最適合發展GWT應用程序GWT MVP架構優勢
我也看過,它很容易做到使用MVP ARCH.Can測試已經閱讀有人向我解釋爲什麼使用MVP架構輕鬆進行測試。
另外我正在使用MVP的項目,我發現它非常繁瑣,使視圖連接到數據base.I的意思是我必須更新我的演示者,服務,serviceAsync,servicImpl,Facades,以便建立連接到數據庫。因此,有人可以爲我提供GWT的MVP的本質嗎?我會欣賞幾個例子。演示者之間
分離(包含邏輯)和視圖(圍繞UI控件啞包裝器)可以:
後者的使用情況是罕見的,因此讓我們集中在MVP模型是否適合自動化的,程序化測試。對於一個開發團隊來說,這通常採用在無頭服務器上使用Hudson(或類似)的連續構建/測試循環的形式,每次運行測試時,打開Web瀏覽器,創建控件等都是不現實的。
MVP + GWT的典型用法是視圖實現了演示者提供的接口,並且通常這個接口是用其他通用接口定義的。下面是增加一個按鈕被點擊時,一個數字標籤,一個非常簡單的演示 - 注意,不是露出TextBox和Button直接,視圖返回通用HasText和HasClickHandlers實例:
public class ButtonClickPresenter {
public interface View {
HasText currentValue();
HasClickHandlers incrementButton();
}
private final View myView;
private int currentValue = 0;
public ButtonClickPresenter(View myView) {
this.myView = myView;
this.myView.currentValue().setText("0");
this.bind(); // for the sake of demonstration
}
public void bind() {
this.myView.incrementButton.addClickHandler(
@Override
new ClickHandler() {
void onClick(ClickEvent event) {
currentValue ++;
myView.currentValue().setText(
Integer.toString(currentValue));
}
});
}
}
「真正的」視圖返回UI窗口小部件(在本示例通過UiBinder的創建):
public class ButtonClickView implements ButtonClickPresenter.View {
// ... skipped UiBinder initialisation ...
@UiField Label currentValueLabel;
@UiField Button incrementButton;
@Override
public HasText currentValue() {
return currentValueLabel;
}
@Override
public HasClickHandlers incrementButton() {
return incrementButton;
}
// ... etc ...
}
而單元測試創建一個虛擬的執行(或使用Mockito,EasyMock等),因此不需要任何用戶界面組件:
public class ButtonClickPresenterTest {
ButtonClickPresenter presenter;
ClickHandler currentHandler;
String currentText;
@Before
public void setUp() {
presenter = new ButtonClickPresenter(
// dummy view - just stores label text in a String and
// keeps track of the Presenter's click handler
new ButtonClickPresenter.View() {
@Override
public HasText currentValue() {
return new HasText() {
@Override public String getText() { return currentText; }
@Override public void setText(String text) { currentText = text; }
};
}
@Override
public HasClickHandlers incrementButton() {
return new HasClickHandlers() {
@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
currentHandler = handler;
}
};
}
});
}
@Test
public void testIncrement() {
// initial value
assertEquals("0", currentText);
// clicking the button should increment the number
currentHandler.onClick(null);
assertEquals("1", currentText);
}
}
至於你的下一段:你的意見不應該連接到數據庫!演示者應通過Service/ServiceAsync(或抽象(如或gwt-platform))請求數據,然後調用視圖上的方法來填充UI。
順便說一句,如果你正在尋找GWT MVP代碼示例 - 結合Google GIN,那麼最後兩個鏈接(以及gwt-presenter)是一個很好的開始,它們提供了將所有這些東西捆綁在一起的框架。儘管如此,我同意 - GWT + MVP + Java的組合可以是艱苦的工作,並且非常詳細(我很高興我現在不需要使用它)。然而,另一種選擇則不那麼有吸引力:一種不可測試的,無法維持的意大利麪條球......
你可能想看看示例gwt-platform應用程序在這裏博客 - >http://uptick.com.au/content/serendipity-working-gwt-platform-and-smartgwt。
Cheers Mark
太棒了!!謝謝 – Barry 2010-10-24 18:58:40