MVC in Java - beyond Swing?
The Model-View-Controller (MVC) paradigm provides a crucial insight into what goes on in an interactive application. However while implementing the model is fairly straightforward, no one seems quite sure what to do about the view and controller.
A case in point is Swing, where many components have pluggable models. The components themselves usually combine view and controller functionality, though the Swing documentation is shy of admitting as much. The result is a sort of 'binary' version of MVC ie M+VC.
A more serious weakness of Swing is that it does not present a complete model-based API for use by client code. The more complex components have models, but Swing does not model simple text strings or floating-point numbers; models tend moreover to abstract their GUI components rather than the data they contain.
In a sense this is the underlying cause of the ongoing Swing data binding problem as discussed in Karsten Lentzsch's recent On Binding posting - Swing does not provide any straightforward way of linking data with the GUI that exposes it.
While Swing as described above merges view with controller into a binary M+VC, patterns experts such as Martin Fowler (see Karsten's posting) offer a variety of takes on the issue. Some of Martin's GUI patterns papers such as http://martinfowler.com/eaaDev/ModelViewPresenter.html adopt an orthodox MVC-like ternary approach, others such as http://www.martinfowler.com/eaaDev/PresentationModel.html are more binary.
I've been working on this topic for a couple of years and the results are at http://superficial.sourceforge.net. Superficial explicitly takes a binary approach to MVC, merging view with controller as follows:
* An abstract application surface provides the user with view and control of application content and logic.
* Content and logic elements are abstracted as targets (aka models) of the individual facets of the surface, which in turn manage the widgets of the concrete GUI.
* A mediating targeter tree maintains the all-important consistency of view and control between facets and targets, and thus between surface and application.
Thus while Superficial adapts MVC for use in GUI applications in an essentially similar manner to Swing, its more comprehensive approach can provide the complete model-based API that is missing from Swing.
Superficial is very much a work in progress, but it does have
* a spike application http://superficial.sourceforge.net/fullspike.html showing how it handles tricky issues such as custom viewers and multiple content selection
* a detailed design and coding example http://superficial.sourceforge.net/design-code.html based on the running example used by Martin Fowler
Both Karsten - who suggested this posting - and XP guru Ward Cunningham have (amongst others) made encouraging comments on the Superficial approach. I'd be interested to hear what the JavaDesktop community thinks of it.