MVP For GWT 系列资料九:Base presenter and view classes for gwt-presenter

源文转自:Base presenter and view classes for gwt-presenter

 

When your presenters extend gwt-presenter’s WidgetPresenter, you are required to implement four methods that are often unused. In order to avoid boilerplate in all my presenters, as well as to do other things that are common to all my presenters, I’ve created an abstract base class that extends WidgetPresenter.

 

public abstract class MyBasePresenter<D extends
MyBasePresenter.Display> extends WidgetPresenter<D>
{
       public interface Display extends WidgetDisplay
       {

       }

       public MyBasePresenter(final D display, final EventBus eventBus)
       {
               super(display, eventBus);
       }

       @Override
       protected void onBind()
       {
               // TODO Auto-generated method stub

       }

       @Override
       protected void onPlaceRequest(PlaceRequest request)
       {
               // TODO Auto-generated method stub

       }

       @Override
       protected void onUnbind()
       {
               // TODO Auto-generated method stub

       }

       @Override
       public void refreshDisplay()
       {
               // TODO Auto-generated method stub

       }

}

 

Now instead of extending WidgetPresenter directly, I can extend MyBasePresenter:

 

public class ManageListsPresenter extends
MyBasePresenter<ManageListsPresenter.Display>
{

       public interface Display extends MyBasePresenter.Display
       {
        ...
       }

       @Inject
       public ManageListsPresenter(final Display display, final EventBus eventBus)
       {
               super(display, eventBus);
               bind();
       }
        ...
}

 

In similar fashion, I define a corresponding BaseView that eliminates the need for the startProcessing() and stopProcessing() methods required by gwt-presenter’s WidgetDisplay interface. Besides eliminating boilerplate, I use the base view constructor to inject references to my applications Constants and Images singletons (see this previous post):

 

public abstract class MyBaseView implements MyBasePresenter.Display
{

	protected final RoaConstants constants;
	protected final RoaImages images;

	protected BaseView(final RoaConstants constants, final RoaImages images)
	{
		this.constants = constants;
		this.images = images;
	}

	@Override
	public void startProcessing()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void stopProcessing()
	{
		// TODO Auto-generated method stub

	}

}

 

Now all views that extend MyBaseView will have less boilerplate and will automatically have access to the constants and images interfaces.

 

public class ManageListsView extends MyBaseView implements ManageListsPresenter.Display
{

	@Inject
	public ManageListsView(final RoaConstants constants, final RoaImages images)
	{
		super(constants, images);
        ...
	}
    ...
}

后记:

I made a serious mistake in my previous post and spent all evening looking for it

A side effect of the if block in the previous post is that the PlaceManager doesn’t get loaded if the URL is empty. I’ve now corrected it to always load the PlaceManager, as gwt-presenter doesn’t work at all without it.

 

你可能感兴趣的:(classes)