You are on page 1of 14

Model-View-Controller

6 April 2013!

OSU CSE!

1!

MVC Design Pattern


! The dominant approach to organizing software with GUIs is the model-viewcontroller design pattern ! There are several versions of this design pattern
! We illustrate one that is very clean ! There should be interfaces for the model, view, and controller classes, but they are left out here only to keep the sample code smaller
6 April 2013! OSU CSE! 2!

Example: Simple MVC GUI Demo


Swing Components JFrame ActionListener

extends

implements

View

6 April 2013!

OSU CSE!

3!

Example: Simple MVC GUI Demo


So far, its just like the previous GUI demo, except this is called View. Swing Components JFrame ActionListener

extends

implements

View

6 April 2013!

OSU CSE!

4!

Example: Simple MVC GUI Demo


Swing Components JFrame ActionListener

extends

implements Code related to setting up and using GUI widgets is in View; but thats all.

View

6 April 2013!

OSU CSE!

5!

Example: Simple MVC GUI Demo


Swing Components Controller JFrame ActionListener

extends

implements

Model

View

6 April 2013!

OSU CSE!

6!

Example: Simple MVC GUI Demo


Swing Components Controller JFrame ActionListener

extends

implements Code related to the model, or non-GUI aspects, is in Model.

Model

View

6 April 2013!

OSU CSE!

7!

Example: Simple MVC GUI Demo


Swing Components Controller JFrame ActionListener

extends

implements Code that mediates between Model and Viewoften called business logicis in Controller.
8!

Model

View

6 April 2013!

OSU CSE!

Example: Simple MVC GUI Demo


Swing Components Controller JFrame ActionListener

extends

implements

Model

View

instance of this class holds a reference to instance of that class

6 April 2013!

OSU CSE!

9!

Its Demo Time


! The DemoGUI2 project contains a very simple MVC GUI application using Swing ! You can get it at:
http://cse.osu.edu/software/common/DemoGUI2.zip

6 April 2013!

OSU CSE!

10!

Set-up by main: After Constructors

6 April 2013!

OSU CSE!

11!

Constructor sets up observers exactly as with the DemoGUI object in the first demo code; not illustrated here.

Set-up by main: After Constructors

6 April 2013!

OSU CSE!

12!

After view.registerObserver

6 April 2013!

OSU CSE!

13!

Now, Whos In Charge?


! Once main completes in the initial thread, the event dispatch thread executes both the Swing code that monitors user interactions and all callbacks it makes to actionPerformed methods

6 April 2013!

OSU CSE!

14!

You might also like