Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of Swing’s MVC Architecture By Geoffrey Steffens (BCSi), Socket Software, Australia Copyright © Socket Software, 2002.

Similar presentations


Presentation on theme: "Overview of Swing’s MVC Architecture By Geoffrey Steffens (BCSi), Socket Software, Australia Copyright © Socket Software, 2002."— Presentation transcript:

1 Overview of Swing’s 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.

2 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.

3 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 it’s behaviour.

4 MVC – The Model The “Model” contains the data Has methods to access and possibly update it’s 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.

5 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 It’s interaction with the model indirectly causes the View(s) to update

6 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.

7 MVC in Swing… Not quite Swing often merges the View and Controller together into one object. Sun call’s this a delegate. The delegate interprets the user’s 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.

8 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 it’s state –These 3 would be implemented by 3 different classes.

9 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 it’s state –There would be 2 classes: the Model and the Delegate.

10 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 PLAF’s A simple matter of switching the delegate which draws buttons, lists, etc. Replaces AWT’s heavyweight OS-look with a controllable, lightweight one. Tradeoff in performance. AWT was definitely faster.

11 PLAF MVC vs AWT MVC View MVC Controller ComponentUI (a delegate) JComponent (MVC Model) Swing’s PLAFAWT (not MVC!) Component Native OS Peer Platform Native Widget

12 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


Download ppt "Overview of Swing’s MVC Architecture By Geoffrey Steffens (BCSi), Socket Software, Australia Copyright © Socket Software, 2002."

Similar presentations


Ads by Google