MVC Swing GUI Design

Model-View-Controller (MVC)

The Model-View-Controller (MVC) architectural pattern is used in software engineering to allow for the separation of three common features of GUI applications: Towards this end one defines three components of such an architecture, the model, view and controller with the following interactions:
The components are: The interactions correspond to the diagram as follows:
  1. All actions begin in the view through events generated by the user. The controller provides listeners for the events. The controller also has the ability to manipulate the state of the view objects in a way which offers a response to the events generated by the user.
  2. The controller interacts with the model by either requesting information from the data source based on user-generated events, or by modifying the data based on these events.
  3. The model provides the programming interface which the controller must use to access the database, i.e., the controller does not interact directly with the database. In particular the notion of "connection" is never seen in the controller. Furthermore, the model also provides means to avoid, in most cases, direct SQL queries to the database.
  4. The view interacts with the model only to know about type information and other data abstractions held in the model. The relatively weak link indicates that the View/Model interaction should be "minimal" because the manipulation of data is to be done within the event handlers of the controller.

Model and ORMs

The model provides a data abstraction, presumably so that one can change databases with little or no modifications to the controller code. JDBC can serve as a model in itself because it provides database independence. However JDBC, in its usage of connections and reliance on SQL, is still considered to operate on a low level. It is conceivable that the data source is not a relational database at all, in which case JDBC would be irrelevant.

In general situations, the model presentation is referred to as an Object Relational Mapping (ORM). The selling feature of an ORM is that it provides convenience methods for automatically treating database records as objects and table accesses as class methods. Although it is impossible to eliminate all direct SQL accesses, it is possible to do so for most common database accesses. ORM packages can be of a "homegrown" sort where classes and methods are created primarily as needed to suit the target application, or of a general purpose nature which can fit any application.

Controller and View

The code which drives GUI applications is event-driven. In Java, the event generators are Java objects which should reside in classes considered to be part of the View. The event handler code should reside in classes considered to be part of the Controller. For the most part this is an easy distinction in that classes derived from Java Swing components belong to the view.


© Robert M. Kline