Views
A key concept of MVP development is that a view is defined by an
interface. This allows multiple view implementations based on client
characteristics (such as mobile vs. desktop) and also facilitates
lightweight unit testing by avoiding the time-consuming GWTTestCase.
There is no View interface or class in GWT which views must implement or
extend; however, GWT 2.1 introduces an IsWidget interface that is
implemented by most Widgets as well as Composite. It is useful for views
to extend IsWidget if they do in fact provide a Widget. Here is a simple
view from our sample app.
public interface GoodbyeView extends IsWidget { void setName(String goodbyeName);}
The corresponding implementation extends Composite, which keeps
dependencies on a particular Widget from leaking out.
public class GoodbyeViewImpl extends Composite implements GoodbyeView { SimplePanel viewPanel = new SimplePanel(); Element nameSpan = DOM.createSpan(); public GoodbyeViewImpl() { viewPanel.getElement().appendChild(nameSpan); initWidget(viewPanel); } @Override public void setName(String name) { nameSpan.setInnerText("Good-bye, " + name); }}