Skip to main content

MVC in Java - beyond Swing?

No replies
Joined: 2005-08-11

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 adopt an orthodox MVC-like ternary approach, others such as are more binary.

I've been working on this topic for a couple of years and the results are at 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 showing how it handles tricky issues such as custom viewers and multiple content selection

* a detailed design and coding example 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.

David Wright