Java & MVC Architecture | Back | TOC |
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:
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:
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