Tutorial 4: The State Patterns


Introduction

This tutorial covers:


I. Smart Word

Create a local directory tutorials/04 for this lab and copy the files ~comp212/tutorials/04/*.java.  These are the Java stub code for the design of a "smart word" given by the following UML diagram.

SmartWord.png (23143 bytes)

In the above diagram, AWord is an abstract class to represent the input word in the hangman game.  An AWord can be empty or non-empty. When it is empty it contains nothing. When it is non-empty, it contains a character and another AWord. The characters in a non-empty AWord can be hidden or visible. When it is hidden, its String representation is "_". When it is visible, its String representation is the String representation of the character itself.  AWord should have enough intelligence to interact with the client as decribed below.

public abstract class AWord
{
    /**
    * Matches an input character with all the characters in this AWord, and returns
    * true if there is a match, false otherwise.
    * Side effect: Makes all matching characters visible.
    * @param c the character to be matched.
    * @return true if there is a match, false otherwise.
    */
    public abstract boolean matchAll(char c);

    /**
    * Helper method for match. Called by the non-empty patrent AWord to help
    * match the remaining characters in the word, given a boolean flag marking
    * whether or not there is a match so far.
    * @param c the character to be matched.
    * @param b true means there is a match so far, false means there is no match so far.
    * @return true if there is a match, false otherwise.
    */
    protected abstract boolean helpMatchAll(char c, boolean b);

    /**
    * Returns true if all characters are visible, false otherwise.
    */
    public abstract boolean isAllVisible();
}

II. Smart Char

The characters in a concrete non-empty AWord are "smart" and know how to compare themselves with other char.  They are implemented using the state pattern.   The following specifies their behaviors.

class WordChar
{
    private char _value;
    private ACharState _state = Hidden.Singleton;

    /**
    * Initialized to c and the hidden state.
    */
    WordChar (char c)
    {
        _value = c;
    }

    final public String toString ()
    {
        return _state.toString (_value, this);
    }

    /**
    * @param c the character to be compared with.
    * @param b true means there is a match so far, false otherwise.
    * @return true if c == _value (in which case _state becomes Visible), otherwise b.
    */
    final boolean compare (char c, boolean b)
    {
        return _state.compare (c, b, _value, this);
    }

    /**
    * Returns true if this WordChar is visible, otherwise false.
    */
    final boolean isVisible ()
    {
        return _state.isVisible (this);
    }

    /**
    * Sets this WordChar to a given state.
    * @param state a new state.
    */
    final void setState (ACharState state)
    {
        _state = state;
    }
}

Exercises:

1. Write the code for Hidden.

2. Write the code for Visible.

3. Write a test for WordChar.

4. Write the code for EmptyWord.

5. Write the code for NonEmptyWord.

6. Write a test for AWord.

Your labbies will give you guidance but will not help you write the code.  The upcoming programming assignment will ask you to write a pogram to play hangman.  The above exercises constitute a non-trivial part of this assigment.

dxnguyen@cs.rice.edu
revised 09/25/00