TurboManage

David Chandler's Journal of Java Web and Mobile Development

  • David M. Chandler


    Web app developer since 1994 and Google Cloud Platform Instructor now residing in Colorado. Besides tech, I enjoy landscape photography and share my work at ColoradoPhoto.gallery.

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 228 other followers

  • Sleepless Nights…

    October 2009
    S M T W T F S
    « Sep   Nov »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Blog Stats

    • 1,007,779 hits

Archive for October 10th, 2009

Base presenter and view classes for gwt-presenter

Posted by David Chandler on October 10, 2009

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);
        ...
	}
    ...
}

Posted in Google Web Toolkit, Model-View-Presenter | 8 Comments »

 
%d bloggers like this: