MVC and Java

May 17, 1998. This article is a little bit old, but I've gotten feedback from people who have been helped by it so I'm keeping it online. I guess the biggest news for Java and MVC is that Sun's Swing user interface is based on MVC and has rather good documentation on MVC and other topics.

In early April 1996 I was interested in developing simulation applets in Java and having heard of the "Model-View-Controller" paradigm I posted to comp.lang.java asking about it. Here are the replies I received:


Date: Wed, 10 Apr 1996 09:24:15 -0700
From: Nolan.Toone@Eng.Sun.COM (Nolan Toone)
To: paul@honeylocust.com
Subject: Re: Where can I find out about Model-View-Controller?
Organization: Sun
I don't know where there is any information on it but I had to teach
myself. Here is a quick summary of what I can tell you about it:

Model - only one. It represents the actual object.

View - from 0 - infinity. usually one. It represents a paticular view of the
       model.

Controller - from 0-infinity, again usualy one and that one is usualy 
        associated with the view above.

It is VERY easy to get functionality mixed up in this because many times
the application is set up to have only one of each. Then later on when you
want to have more Views or Controls things start breaking down fast.

If your doing it with Java check out the Observer/Observable Objects
where the Observable is the model and Observer is the View. It is not
everything you need but it helps to do the syncronization of the views
with the model.

Good Luck,
Nolan Toone
SunSoft, Inc.

To: paul@honeylocust.com
From: br.mjc@rlg.org (Michael Carroll)
Subject: Re: Where can I find out about Model-View-Controller?
Date: Wed, 10 Apr 1996 10:17:41
There's some discussion of MVC on pp. 176 et seq. of Brad Cox's 1991
"Object Oriented Programming", 2nd ed., but he doesn't give any
references, and I don't know of anything more recent.  Let me know
if you hear of anything.

Michael Carroll
Research Libraries Group
br.mjc@rlg.org

From: cd001748@interramp.com
Date: Wed, 10 Apr 1996 18:46:53 -0800
To: paul@honeylocust.com
Subject: Re: Where can I find out about Model-View-Controller?
The classic MVC article is: Krasner and Pope (1988), A cookbook for
using the Model-View-Controller user interface paradigm in
Smalltalk-80. Journal of Object-Oriented Programming.1(3)
Combining the View-Controller as well as the classic structure is
discussed in Wolfgang Pree (1994), Design Patterns for
Object-Oriented Software Developement, Addison-Wesley.


Date: Thu, 11 Apr 1996 12:24:27 -0700
From: mailto:doan_j@sat.mot.com (Jake)
To: paul@honeylocust.com
Subject: Model-View-Controller

Paul,

You can find reference to this topic in the Gang of Four (GOF) 
Design Pattern book.  In addition, there's a Siemen pattern
site which also include coverage on the same topic.  It's at


http://st-www.cs.uiuc.edu/ftp/pub/patterns/siemens/


Check it out!


**********************************************************
Jake P. Doan
Motorola SatCom/IRIDIUM Inc.
Email address: mailto:doan_j@sat.mot.com
**********************************************************

From: Staypufed@aol.com (Staypufed)
Date: Fri, 12 Apr 1996 01:36:56 -0400
To: paul@honeylocust.com
Subject: Re: Where can I find out about Model-View-Controller?

The book "Smalltalk - The Language" by Goldberg and Robson explains the
critical part of MVC which is dependencies.  (The Java equivalients are
Observable and Observer)

MVC - Model-View-Controller

The model is your set of physics classes
The view and controller are merged in Java.

The basic idea is that you have your views register as dependents of the
model.  When the model changes it notifies all dependents (e.g. - Those
who've registered) and then those objects can then react.  In the case of
views they can redraw.

The dependency notification is through the msg notifyObservers.  All
objects that are observers need to implment the update mthd b/c this is
what the notifyObservers msg will turn around and send to all the
registered observers.

Note:  Dependents can be other objects also.

hope this helps,


Sam Griffith Jr.
Staypufed@aol.com


To: paul@honeylocust.com
Subject: Re: Where can I find out about Model-View-Controller?
Date: Sat, 13 Apr 1996 07:33:25 -0700
From: Gregory Alan Bolcer
 > Hi,  I'm writing some physics simulation applets (I will
 > probably have them webbable by the end of the week) and I've hit
 > upon an architecture that somebody said was a lot like something
 > called the Model-View-Controller paradigm.
 > 
 > 	Anyway,  I have been looking for some basic introduction to
 > the MVC paradigm,  although I haven't been able to find anything
 > cogent about it on the web or in print.  Can anybody direct me to
 > a good starting place?

Hi Paul,
  By the date on this email, you've probably received hundreds 
responses and have gone on to build your applet, but for what it's
worth...Model=your data object, View=you applets, 
Controller=java.util.Observable.

Subclass your data classes from java.util.Observable.  For
each applet that is interested in displaying the data, build the
class and have it 'implements Observer' meaning it implements
the update() function call and call Observable.addObserver. 
Whenever your data changes, possibly from a write event from 
your simulator or interaction from the user, you can notify your
observers in several ways: 1)Seed your data object to call
Observable.notifyObjservers() at key points in the code, 2)
seed your code with Observable.setChanged() and have your
observers poll to find out if the data Observable.hasChanged(),
or 3) build a 'wrapper' subclass of your object that notifies
the registered applets when something interesting (to them) has happened.
Once the observer applets receive the event, they can query your
data object with non-changing calls.  There are all sorts of
ways you can extend java's MVC, by filtering events, registering
for specific interface calls, registering for specific data
values, etc. but they involve subclassing the controller, ie.
Obervable.  

Greg


Copyright © 1997 honeylocust media systems, contact paul@honeylocust.com