UMBC CMSC 331 Principles of Programming Languages

Bouncing Ball Applet

Model-View-Controller

adapted from an example by David Matuszek

This is a simple example of Model-View-Controller uing the Observer/Observable technique. It displays a bouncing abll and has a single button labeled STWP. Clicking the button advances the model one step.

The Model does the work. Whenever it does something that another class might be interested in, it "broadcasts" the fact to all other classes that might be interested. Note that the Model does not need to know or care anything about which other classes, if any, might be interested, so it remains completely independent of those classes.

The View is interested in observing the Model class; it does this by implementing a version of the update method, which will automatically be called any time a class that it is observing broadcasts something.

The Controller in this program doesn't actually have any controls. However, it does set up the GUI, create the Model and the View, and register the View as an observer of the Model.

This code is available as a BlueJ project in a zip file.