Question

MVP paradigm assumes that the model and view are connected via the presenter, but CellTable by default requires a type argument in its constructor. So does this mean CellTable cannot be made to fit MVP?

Was it helpful?

Solution

I have successfully used CellTable in MVP. The key is to understand the purpose of this architecture.

MVP is not about the view being completely agnostic to your model, it is fine under MVP to have a view that will only work for your specific model classes. The main point is to keep as much of the model handling and event handling as possible in the presenter. This allows presenter code to be efficiently unit tested. Anything that requires a browser environment (e.g. widgets) should be in the view, where it will not interfere with normal unit testing (such components can only be tested with GwtTestCase, which is very slow).

I have a View interface method to create the table, which is returned as HasData. In the presenter bind method, I call this method to get the table, instantiate the data provider for the table, and manipulate the data provider in the presenter with any updated data.

//MyPresenter.java
HasData<MyClass> table = display.addTable();
ListDataProvider<MyClass> dataProvider = new ListDataProvider<MyClass>();
dataProvider.addDataDisplay(table);

//assumes results is a collection of MyClass to display.
//use for loop if you need to do some manipulation to get the objects in
dataProvider.getList().addAll(results);


//MyView.java
@Override // since this implements the definition from the view interface
public HasData<MyClass> addTable()
{
    CellTable<MyClass> table = buildTable(); //boiler-plate table building code in this method
    myPanel.add(table);
    return table;
}

This allows me to keep the view logic (column definitions and such) in the view where it belongs, and deal with the data model in the presenter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top