CS 204: Software Design
MVC with a very simple M
Of all the patterns in our Design Patterns book,
the one I run into most often in my day-to-day programming life is the Model, View, Controller (MVC).
In this assignment, you will implement a simple example of the MVC architecture, using several of Java's graphical
user interface elements.
Your program from the user's perspective.
When you launch your program, a window will open, showing four controls:
a spinner,
a slider,
a text box,
and a thermometer of your own design. Each of these four objects will represent the same integer
between 0 and 1000. At startup, the integer will be 500.
If you click on the thermometer at the height of, say, the integer 245, the text box, spinner,
and slider will all change to match 245. If you type a new integer in the text box, or slide the slider,
or spin the spinner, the other three controls will change to match you as you type, slide, or spin.
At any given time, all four controls will reflect identical integers.
What to hand in
Hand in source code and a readme. The readme should explain your
class design, your command-line syntax, and the status of your program.
Notes and suggestions
We will be doing another round of code reviews with this program, but not
a rewrite.
Some suggestions:
- Create a class design on paper first. You'll need a model, four views, and either
one or four controllers, depending on what you think the controller needs to do.
Your model should have an integer instance variable to store the current integer.
Maybe the model will have more instance variables, maybe not--that's one of the places
you will be making design decisions.
- You will need to get the hang of creating, laying out, controlling, and responding to
the various GUI elements made available to you in the Java libraries. Study the
tutorials I've linked to above, and the sample code that comes with them.
- When you learn from other people's sample code, don't copy their code uncritically.
Sometimes their code will be a mess, or will do something in a complex way when a
simple way will do, etc. Often, the thing I really need to learn from a 200-line sample
is one or two lines. When that happens to you, just copy those two lines--not the whole
infrastructure of the sample. And of course, cite your sources.
- There are idiosyncratic problems with each of the GUI elements you will be
using. For example, with the text box, you'll need to decide what should happen if
you type non-digits in the box. For the thermometer, you may be able to use
existing Java tools, or you may need to draw the thermometer yourself. Work up
these problems by doing the easy parts first, and making simplifying assumptions
if possible (e.g. don't worry about the non-digit typing until later).
- Build and test your initial architecture around the easiest controls. I'd recommend
getting the slider and spinner to work with each other first, and then worry about
the other two controls and their particular problems later.
- This assignment is about the Model-View-Controller pattern. Don't succumb to the
temptation to put everything in one class, or rearrange things because you think
MVC doesn't apply here, or whatever. You're going to encounter MVC out there in the
world--it will help you a lot to have implemented it once yourself.
Are you crazy?
If you have tons of spare time and are dying to do something fancier, you can replace
"integer" with "time of day" in your model and your views. In this case, the thermometer would
become a circular analog clock, the text box would become a pair of text boxes with a colon between them, etc.
Doing this version won't be worth more points, but if you want to spend your time figuring
out how to drag the hands of a clock around with a mouse, go right ahead.