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.

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值