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 224 other followers

  • Sleepless Nights…

    October 2009
    S M T W T F S
  • Blog Stats

    • 1,034,589 hits

Factoring out common MVP services into a Service Facade

Posted by David Chandler on October 5, 2009

Shortly after adopting the Model-View-Presenter pattern, I found myself copying the same service call into multiple presenters, as both a navigation panel and a main content area can sometimes display the same information. I decided to factor out the repeated service calls into a Service Facade (for lack of a better name) and inject the facade into each presenter that’s needed.

	public AddPrayerListPresenter(Display display, EventBus eventBus, PrayerListServiceFacade prayerListServiceFacade)
		super(display, eventBus);
		this.prayerListServiceFacade = prayerListServiceFacade;

Inside the service facade, I use constructor injection to get the event bus and dispatcher the same as in a presenter.

	public PrayerListServiceFacade(final EventBus eventBus,
			final DispatchAsync dispatch, RoaModel roaModel)
		this.dispatch = dispatch;
		this.eventBus = eventBus;
		this.model = roaModel;

	public void refreshPrayerLists()
		GWT.log("Calling actionFindLists", null);
		// Fire event that marks start of the service call so listeners
		// can show the AJAX wait thingy if desired
		eventBus.fireEvent(new RefreshingPrayerListsEvent());
		dispatch.execute(new FindPrayerListsAction(),
				new AsyncCallback<FindPrayerListsResult>()
					public void onFailure(Throwable e)
						GWT.log(e.getMessage(), e);

					public void onSuccess(FindPrayerListsResult result)
						eventBus.fireEvent(new PrayerListsModifiedEvent(result.getPrayerLists()));

The refreshPrayerLists() method contains the code that had been common to multiple presenters. There is one difference, however. When a presenter calls a service, it typically passes a DisplayCallback object, which gwt-presenter uses to call the view’s startProcessing() and stopProcessing() methods so you can show an Ajax wait thingy (er, progress indicator?) I could add a Display argument to the service method and create a DisplayCallback as usual; however, in this case, I want the refresh method to be called only once in conjunction with the event that necessitates a refresh (say, a new prayer list is added). And I want multiple widgets to be notified when the service call begins, and when it finishes. To accomplish this, I pass only a regular AsyncCallback object and fire an event both before and after the service call, to which any interested widget can listen.

In summary, the service facade provides a way to group all related service calls in one place and to factor out common calls from multiple presenters. Now all I need is a better name for it, as it sounds a little scary for my taste…

7 Responses to “Factoring out common MVP services into a Service Facade”

  1. Now all I need is a better name for it, as it sounds a little scary for my taste…
    I use a similar pattern on my project. I just call it a *Service and *ServiceImpl – allowing me to mock it out when writing tests with EasyMock.

    • Thanks, Matt. I had wanted to avoid confusion with GWT-RPC services, but come to think of it, with gwt-dispatch, there’s only one real service, so there won’t be any naming confusion, after all. And the subtle suggestion to use an interface also is well-taken.

  2. Darren said

    Very neat. I’m finding your blog a very interesting read. 🙂

  3. Roar Skullestad said

    Just curios, how is RoaModel implemented? I see from another post that it contains a getLoginInfo() method. How does it use DispatchAsync? Does it always contact server or does it work like a cache?

  4. lost said

    what exactly is a PrayerList ?!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: