Fabrizio Giudici is a Senior Java Architect with a long Java experience in the industrial field. He runs Tidalwave, his own consultancy company, and has contributed to Java success stories in a number of fields, including Formula One. Fabrizio often appears as a speaker at international Java conferences such as JavaOne and Devoxx and is member of JUG Milano and the NetBeans Dream Team. Fabrizio is a DZone MVB and is not an employee of DZone and has posted 67 posts at DZone. You can read more from them at their website. View Full User Profile

A Cleaner MVC Inspired by Continuation-Passing Style

  • submit to reddit

With this article I'm starting a series derived from how I'm designing blueBill Mobile, an Android application, but in addition to technology-related topics (not only Android) I'll talk a lot about general design strategies. A relevant effort has been made to define a design that makes testing easier and also reduces the technology-relevant part of the code to a minimum.

In fact, the whole application is being developed as a core which is just plain Java and can be implemented by different UI solutions, such as Android, web apps and desktop; examples will be given in future articles which cover also Vaadin, Wicket (for the web part) and the NetBeans Platform (for the desktop part). The articles will focus on different topics case by case, moving from design considerations to lower level details concerning the implementation, even including tools, such as Maven, the NetBeans IDE, the maven-android-plugin and the NetBeans Android plugin.

In this first article we're going to deal with design considerations about the use of MVC together with some ideas inspired from a technique callled Continuation-Passing Style (CPS). The last part of this article deals with testing and assumes prior knowledge of Mockito.

I have to thank many friends at JUG Milano and at the JavaPosse for having helped me to better understand CPS and the correct way to refer to it.

Code examples are mainly taken from blueBill Mobile for Android, version 1.0-ALPHA-3.

1. Some specifications

Let's start with a simple thing. blueBill Mobile contains a small RSS feed reader, called "News", which is used to deliver to users posts from the project blog. The requirements are easy:

  1. The user enters a specific screen and sees a list of topics (with caption and publication time).

  2. Each topic can be marked as read or not read by means of a context menu and it's rendered in a different way to reflect its state.

  3. A topic can be selected so it can be read in full (and will be marked as read).

  4. The feed can be cached in memory in case there's no network connection, or the user doesn't want to connect.

  5. A preference says whether the application is allowed to connect to the internet.

  6. If network connections are allowed, the news service retrieves a fresh copy of the feed.

  7. If network connections aren't allowed and a cached copy is stored in memory, news are rendered with a warning and no attempt is done to retrieve a fresh copy.

  8. If network connections aren't allowed and no cached copy is available, the user is explicitly asked for the permission to connect. If it doesn't confirm, the interaction terminates.

  9. Unexpected errors (such as network connection failures) are be properly reported.

It's pretty simple, but it makes it possible to explore some design directions about the control flow moving from the backend (the news service) and the user interface (confirmations), as well as strategies for testing.

it.tidalwave.bluebill.mobile.news_.png32.91 KB
it.tidalwave.bluebill.mobile.news_.ui_.png13.07 KB
package.png12.46 KB
UserNotificationWithFeedback.png21.21 KB
Published at DZone with permission of Fabrizio Giudici, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)