Your Names:

6.831 • User Interface Design and Implementation

Massachusetts Institute of Technology
Department of Electrical Engineering and Computer Science
Fall Semester, 2006

AC4: Models, Views, and Events

The purpose of this activity is to explore some of the subtleties of the MVC pattern and event-driven architectures. You need a web browser and the Java Development Kit installed on your laptop to do this exercise.


1 MVC in Swing

Identify all the instances of model-view-controller you can find in the WordFinder program you had to write for PS1 (shown at right). For each MVC, draw an object diagram showing the participants as circles labeled by class name and role (M,V,or C). Draw arrows between them to represent their dependencies, using solid lines for direct method calls and dotted lines for event-passing.

It may help to look at PS1 again, and to look at the Java 1.5 API documentation.

2 Generating Events

Consider this Java code, found inside some model class:

  private Set<SomeListener> listeners = new HashSet<SomeListener>;
  public void addSomeListener(SomeListener l) { listeners.add(l); }  
  public void removeSomeListener(SomeListener l) { listeners.remove(l); }

Louis Reasoner says, "The only safe way to iterate through the listeners set is to make a copy of the set every time you need to fire an event." What danger is Louis worried about?

Ben Bitdiddle replies, "But that's not efficient -- you could make a copy only when you absolutely have to." What does Ben mean? How would you keep track of when you have to make a copy?

Alyssa Hacker adds, "I use javax.swing.event.EventListenerList for managing event listeners." How does EventListenerList deal with the problem that Louis raised? You may be able to guess by reading its API documentation carefully, but to be sure, look at the source code for EventListenerList, either by opening up in your Java directory, or by using Eclipse to drill down into JRE System Library / rt.jar. Knowing where to find the source code can be very handy for debugging.