Comp 212 Lab 03: Visitor Pattern


This tutorial consists of exercises on the visitor pattern discussed in class.  It also helps prepare for homework 03.  

I. To Drive or Not To Drive

The  UMLclass diagram below models keys and cars using the visitor pattern.  Though AKey is depicted here as an abstract class, it can also be an interface.  

You are to do the following:

1. Fill in the concrete methods of all the above concrete classes so that only a specific key will drive a specific car when that car's turnKey() method is called with that key, i.e. call its drive() method. All other keys will call the car's alarm() method.

For instance, suppose we have:

then ford.turnKey(fordKey) will cause to be called.

On the other hand, suppose instead we have

then ford.turnKey(saturnKey) will cause ford.alarm() to be called.

2. Fill in the and ACar.alarm() methods to print out something indicative that the method has been called, e.g. "Car has been driven!" and "Car alarm sounded!".

3. Document your code, of course. It is recommended that you document the methods before you write the method bodies! (Remember Comp210?)

3. Write a JUnit test class that clearly tests all possible key and car combinations. Be sure that the test methods print clear messages indicating exactly what is being tested at the moment, i.e. what key-car combination is being used.

II. Student's Social Life

Create the object system that models the following using the Visitor Design pattern:

You're sitting in your"working"....or maybe not..... Suddenly, the phone rings and some person is on the other end. You need to proffer a response, but the exact response behavior will depend on not only what you've been doing, but who the person on the line is.

Now, there are a couple of scenarios:

  1. You've been working diligently on your Comp212 homework and the person calling is:
    1. Your mother. You want say "Oh yes, Mom, I've been working so hard, I haven't got time to even think about going to parties!"
    2. Your Comp212 lab-mate: You want to say "Problem 4's a piece of cake, once you understand double-dispatching!"
    3. Your significant other. You want to say "I'll be done in a minute!"
  2. You've been vegging out watching re-runs of the Gong Show and the person calling is:
    1. Your mother. You want say "Of course, I'm working hard! By the way, did I tell you that books cost more than I thought they would?!"
    2. Your Comp212 lab-mate. You want to say " you have a clue how to do Problem 1?"
    3. Your significant other. You want to say "I'll be there right now!"

Some things to consider:

You are required to do the following:

  1. Create a UML diagram that models the above situation.
  2. Write all the code, including all the method bodies.
  3. Document all your code, of course.
  4. Write a test program that clearly demonstrates that your system properly handles all 6 of the above scenarios. Be sure that the test code prints out helpful messages that make it obvious what scenario is being tested at the moment.

III. List Visitor Exercises

Create a subdirectory called OOscheme.  Click here to copy the (zipped up) OOscheme  code to the OOscheme directory.  Unzip the file.

You will need use the IList framework in the OOscheme package in order to compile and test your code.  You must be in the parent directory of OOscheme to compile all of your code.  Put all the visitors in the package OOscheme.visitor (a package visitor inside the package OOscheme).

  1. Write a visitor called CheckAllNonNegative that checks to see if all elements in the host IList are non negative.  Its methods should return Boolean.TRUE if all the Integer elements in this IList are non-negative (>= 0), Boolean.FALSE otherwise.  Write a simple test class.
  2. Write a visitor called nonNegCount  that computes and returns the number of non-negative Integer elements in this IList.  Its methods should return an Integer.  Write a simple test class.
  3. Write a visitor called checkMostlyNonNeg that checks to see if the host IList contains more non-negative integer than negative ones.  Its methods should return a Boolean.  Write a simple test class.
  4. Write a visitor called Sum to compute the sum of IList, assuming IList contains Integer objects.  Write one version using direct recursion, and one version, called GetSum, using a helper visitor called GetSumHelp.  Write a simple test class.
D. X. Nguyen & S. B. Wong.  Last revised January 25, 2006,