Traffic Light

Goals

The goals of this exercise:

  1. The introduction to Threads.
  2. Introduction to of thinking about a program as an architecture of classes.

The classes:

Discussion:

The first thing we did was to name classes, assigning responsibilities to each class, and specifying their interactions, particularly how objects will reference each other. In this we were guided by the Model-View-Controller pattern: responsibilities were categorized broadly into classes responsible for implementing the conceptual object to be viewed and controlled, classes responsible for making a view of the model, and classes responsible for controller the model.

The architecture we developed was very general; except we did look ahead to the simplicity of what we were to accomplish and did make the following simplifications. First, we noted that the controller aspect would be fulfilled by a viewing class, and hence made TheController an interface, rather than a class. In this way, the class implementing the interface could have two types: the type of the viewer, inheriting from JPanel, and the type of a controller, implementing TheController. Second, we recognized that the model's interaction with the view was limited to the repaint upon a single JPanel, the ModelView, hence introduced a getter upon the TheView and a setter upto TrafficLightModel to communicate this interaction.

Here are the four view classes and their hierarchy.

     TheView ---+-- (add,Border.CENTER) -- ModelView
                |
                +-- (add.Border.NORTH) --- ButtonView --- JButton
Here are the Controller, Model, View interactions:
    TheController ---walk---> TrafficLightModel ---repaint---> ModelView
                                                <--getLight--
Finally, TrafficLight and TrafficLightApplet are two wrapper classes which create and start TheView and TrafficLightModel.
    TrafficLightApplet --- TrafficLight --+--- TheView
                                          |
                                          +--- TrafficLightModel

16 June 2002