Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Design an MVC framework using annotations

Annotations can help decouple your application components

When designing an application, clearly separating the different logic components that define it always proves useful, and many different paradigms help developers achieve that goal. One of the most famous and most used is surely Model-View-Controller (MVC), which divides each application (or part of it) into three different fundamental elements and states the rules for linking them together. Swing itself is based on this pattern, and everyone who has worked with Struts, a popular framework for building Web applications, knows the theory behind MVC.

This article shows how to enhance MVC by adding a new component to the game that uses annotations to provide an easier decoupling between models and views. It introduces an open source library, named Stamps, which is based upon the component and removes the burden of writing all the plumbing between models, views, and controllers when developing MVC applications.

The basics: MVC, and annotations

As the name indicates, the Model-View-Controller pattern suggests the division of an application into the following elements:

  • Model: Contains the data model and all information that identifies the state of the application. It is generally self-consistent and independent of the other elements.
  • View: Stands on the other side in respect to the model and defines the representation of the data stored in the model. The view is commonly identified as your application's user interface (or GUI) or, in case of Web applications, the browser Webpage.
  • Controller: Represents the application logic. Here, it is defined how the user can interact with the application and how user actions are mapped to model changes.

These components are tied together: The user affects the view, which, in turn, notifies the controller, which then updates the model. Finally, the model updates the view to reflect its new state. Figure 1 represents the typical MVC setup.

Figure 1. The typical MVC setup

As one of the new features provided with J2SE 5.0, annotations allow the developer to add metadata to classes, methods, fields, and other language components. Just like reflection, any application can then retrieve and use that metadata at runtime for whatever reason. Since J2SE 5.0 defines only how to write and read annotations, but not what to do with them (with the exception of the predefined ones, like @Override), the developer has endless possibilities in using them for many different jobs: documentation, object-relational mapping, code-generation, and so on. Annotations have become quite popular as most frameworks and libraries are being updated to support them. See Resources for more information on MVC and annotations.

Surpassing MVC: The dispatcher

As mentioned previously, some sort of coupling between models and views proves necessary since the latter must reflect the former's state. Common Java programs use direct or indirect coupling to bind the components together. Direct coupling occurs when the view has a direct reference to the model—or vice versa, the model contains a list of the views to be maintained. Indirect coupling is generally achieved with the adoption of an event-based dispatching mechanism. The model fires events whenever its state changes, and an independent number of views register themselves as listeners.

1 | 2 | 3 | 4 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. My problem is tracking the messages.
By matthew_ford
2 04/22/08 06:02 AM
by Anonymous
. Design an MVC framework
By JavaWorldAdministrator
2 04/22/08 06:02 AM
by Anonymous


Resources