COMP 212 Lab 05: Model-View-Controller Pattern

This tutorial covers:


Model-View-Controller Pattern

Create your own copy of this lab's code, e.g., cp -r ~comp212/public_html/01-spring/labs/05/ .... This contains the Java byte code and some of the source code for a GUI application that can be used to test the classes required for milestone #1 of the Hangman project. Note that we have not provided all of the source code, because that would be completing milestone #3 for you.

Using the Hangman GUI

To see how the application behaves, run the command:

     java WordTest/WordControl
A window called the view should appear. This window has input/output text field at the top, an input textfield at the bottom, a reset button at the right, and an output label field at the left. It is implemented as the class WordGUI.

You can play a game of Hangman, e.g.,

  1. Type the word hello in the top text field and hit enter. The is a sample word to be guessed (e.g., by another person). The input text becomes _ _ _ _ _.

  2. Type the letter l in the bottom text field and hit enter. The top text field should display _ _ l l _ , and the output label field should display Good Guess!. (This demo does not draw a body.)

  3. Erase the letter l in the bottom text field, type the letter a, and hit enter. The top text field should display _ _ l l _ , and the output label field should display Wrong Guess!.

  4. Erase the letter a in the bottom text field, type the letter h, and hit enter. The top text field should display h _ l l _ , and the output label field should display Good Guess!.

  5. You can finish the game if you wish.

Implementing the Hangman GUI

The object that does all the checking of the guesses is called the model. It is represented by the class AWord, its concrete subclasses, and supporting classes. When a user types a guess letter in the bottom text field and hits the enter key, the Java virtual machine creates an appropriate "event" and delivers it to this text field. This text field is programmed to respond to this event by first inquiring the AWord model to match the guess and then updating the other GUI component accordingly. The wiring between a GUI component and the model is called a control. The class WordControl establishes all the controls to wire the GUI view to the AWord model. The design of this small GUI application is fashioned according what is commonly known as the Model-View-Control (MVC) paradigm, as illustrated below.

MVCGUI.png (17164 bytes)

The design consists of several pieces:


Event Handling

For a GUI component to respond to an event, you must attach to it an appropriate event listener object. ActionListener is a special kind of event listener that is associated with GUI components such as JTextField and JButton. You attach an ActionListener to a GUI component by calling addActionListener(...) on the GUI component. The following code fragment for the constructor of WordControl illustrates the attachment of event listeners to GUI components.

     public WordControl()
     {
        // Add controller for hangman word input/output
        _frame.get_tfWordIO().addActionListener(
               new java.awt.event.ActionListener()
               {
                  public void actionPerformed (ActionEvent e)
                  {
                     _aWord = WordFactory.Singleton.makeWord(_frame.get_tfWordIO().getText());
          
                     _frame.get_tfWordIO().setText(_aWord.toString());
                     _frame.get_tfWordIO().setEditable(false);
                  }
               }
           );

        // more stuff...
     }

ActionListener is an interface. As we've seen beforce, that means it is a special kind of abstract class with no method code bodies and no non-static fields It has one (abstract) method:

     public void actionPerformed(ActionEvent e)
A concrete ActionListener must have code for this method, as in the above example. This method will be executed when an ActionEvent is created and delivered to the corresponding GUI component. So, whenever a text is entered into the top text field, _frame.get_tfWordIO(), its ActionListener object will execute its actionPerformed method.

This ActionListener object is created in a very special way here -- it does not have a name and exists only in the context of an instance of WordControl! It is called an anonymous inner class of class WordControl. In the above diagram, the anonymous event listener/handler for the top text field is labeled WordControl$1. Each inner class object has a reference to its outer object WordControl.

The following is a summary of how to design a GUI application in Java using the MVC paradigm:

  1. Design a GUI and expose appropriate GUI components with getter methods (e.g., WordGUI).

    Exercise: Work on this in lab. The standard approach to build a window is to subclass JFrame. The labbies will help you complete the construction of the frame. Also, we will soon have a special tutorial covering Swing, the Java GUI library.

    This is a substantial part of milestone #3 of the hangman project.

  2. Design the model with appropriate intelligence to solve the problem at hand (e.g., AWord and its concrete variants).

    You are implementing this for milestone #1 of the hangman project.

  3. Design a controller to instantiate views and models, and attach event listeners to appropriate GUI components in order to wire the view to the model (e.g., WordControl).

    Exercise: Work on this in lab. The labbies will help you complete the code for the listener to the bottom text field where your guess is entered.

    This is a substantial part of milestone #3 of the hangman project.