You are on page 1of 12

Overview of Swings MVC Architecture

By Geoffrey Steffens (BCSi), Socket Software, Australia Geoff@SocketSoftware.com Copyright Socket Software, 2002. This document is released into the Public Domain. Reproduce at will.

What is MVC?
MVC is an acronym for Model View Controller It represents a software design pattern developed at Xerox PARC in 1978 (!) It explains a method of separating the visual, interaction and data components. Very popular, used extensively in Java and other languages.

Why use MVC?


Makes it very easy to have multiple different displays of the same information. For example: a graph and a table could both display and edit the same data. Essentially provides greater control over the UI and its behaviour.

MVC The Model


The Model contains the data Has methods to access and possibly update its contents. Often, it implements an interface which defines the allowed model interactions. Implementing an interface enables models to be pulled out and replaced without programming changes.

MVC The Controller


Users interact with the controller. It interprets mouse movement, clicks, keystrokes, etc Communicates those activities to the model eg: delete row, insert row, etc Its interaction with the model indirectly causes the View(s) to update

MVC The View


The View provides a visual representation of the model. There can be multiple views displaying the model at any one time. For example, a companies finances over time could be represented as a table and a graph. These are just two different views of the same data. When the model is updated, all Views are informed and given a chance to update themselves.

MVC in Swing Not quite


Swing often merges the View and Controller together into one object. Sun calls this a delegate. The delegate interprets the users interactions and updates the model It also handles the display Note that this still allows multiple views on the same model. It has nothing to do with C# delegates.

Delegates vs MVC An example


The true MVC approach:
A checkbox widget The model contains the checked state The view displays the [un]checked in any manor it decides, eg a checkbox. The controller listens for some sort of activity, interprets it then dispatches to the model. Eg: clicking checkbox inverts its state These 3 would be implemented by 3 different classes.

Delegates vs MVC An example


The Swing delegate approach:
Model is same as MVC example stores state of checkbox. The delegate displays the [un]checked in any manor it decides, eg a checkbox. The delegate listens for some sort of activity, interprets it then dispatches to the model. Eg: clicking checkbox inverts its state There would be 2 classes: the Model and the Delegate.

The MVC Power


One of the best examples of MVC/Delegates in Swing is the pluggable look and feel (aka PLAF) PLAF enables the entire UI look to be changed by simply switching PLAFs A simple matter of switching the delegate which draws buttons, lists, etc. Replaces AWTs heavyweight OS-look with a controllable, lightweight one. Tradeoff in performance. AWT was definitely faster.

PLAF MVC vs AWT


Swings PLAF AWT (not MVC!) JComponent (MVC Model)

Component

Native OS Peer

ComponentUI (a delegate) MVC View MVC Controller Platform Native Widget

More Information
Copious amounts of information on MVC pattern available on Internet. Examples of MVC (Delegate style) all throughout Swing see JDK doc for details Hope the overview was useful to you! More tutorials, Source-code, etc available at our company web site, www.SocketSoftware.com We consult and develop applications in many languages, including Java and Microsoft.NET Australian based, Programming world-wide

You might also like