«no subject»

Jump to: navigation, search

The Model-View-Controller (MVC) architecture is a powerful design pattern that recurs in most object-oriented programming languages and applications. MVC essentially decouples the data model from the component that represents it on the screen. If you have to work with graphical interfaces, you will most likely use MVC pattern in your program. Unlike AWT, the Java Swing component set uses the MVC architecture consistently.

One of the earliest programming frameworks, MVC is a programming idiom to solve the user interface problem. If you write code that handles the data as well as draw the user interface screen to present it, you end up with a kludge. The code becomes error prone and difficult to maintain.

MVC solves the problem by dividing it into three parts:

  1. the data model - the component that handles data in the application
  2. the view - the component that presents the data to the user
  3. the controller - the component that mediates between the model and the view

This way the user interface code is decoupled from the code that handles data, and another component, the controller, is used to provide the interaction between the two. The benefits are two fold:

  • Multiple views are possible from the same data model
  • Flexibility and re-use

The Java Swing set uses a modified MVC pattern. It combines the view and controller parts into a component. Each components has an associated data model, either a built-in class or a user defined sub-class. Explore a bit with the JTable and the JComco classes in the Swing set and you will see what I mean by multiple views, flexibility and re-use.

Related Topics: Observer, Strategy and Composite Design Patterns.
See also Erich Gamma et al, Design Patterns Elements of Reusable Object-Oriented Software

[Published on my website at: http://javanook.tripod.com/patterns/java-mvc.html]

Anandb (talk)20:33, 11 June 2009