Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can, or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing heavily in Java's future as a platform for platforms

Also see:

Discuss: Java: A platform for platforms?

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Design an MVC framework using annotations

Annotations can help decouple your application components

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

java mail decodingBy Anonymous on November 15, 2008, 12:47 ami believe that java mail decording has not been starved as the subline onslought has not been used

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources