Comp 212 Lab 04: Visitor Pattern
Introduction
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
Consider the following UML model of keys and cars:

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:
- An object called ford
of type Ford, and
- An object called fordKey
of type FordKey
then ford.turnKey(fordKey)
will cause ford.drive() to be
called.
On the other hand, suppose instead we have
- An object called
saturnKey of type SaturnKey
then ford.turnKey(saturnKey)
will cause ford.alarm() to be
called.
2. Fill in the ACar.drive()
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 test program that clearly tests all possible key and car
combinations. Be sure that the test program prints 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 room...um...."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:
- You've been working diligently on your Comp212 homework and the person
calling is:
- 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!"
- Your Comp212 lab-mate: You want to say "Problem 4's a piece of
cake, once you understand double-dispatching!"
- Your significant other. You want to say "I'll be done in a
minute!"
- You've been vegging out watching re-runs of the Gong Show and the person
calling is:
- 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?!"
- Your Comp212 lab-mate. You want to say "Uh...do you have a clue
how to do Problem 1?"
- Your significant other. You want to say "I'll be there right
now!"
Some things to consider:
- "Saying something" means printing a string to the console.
- There is more than one solution to this problem! The trick is to clearly
identify the host vs. the visitor.
- Remember that the abstract host contains a method that will accept
(execute) an abstract visitor.
- Use method and class names that correspond to what is happening. Done
correctly, your code should be "self documenting", that is, if you
read your code, it should sound very similar to what the code is really
doing.
- Your code should depend neither on what you were doing nor who is calling!
- In spite of responding without identifying the caller, a bright Rice
student like yourself manages to say the appropriate thing anyway.
- No "if"statements
are needed. Expect significant grading penalties if you use one!
You are required to do the following:
- Create a UML diagram that models the above situation.
- Write all the code, including all the method bodies.
- Document all your code, of course.
- 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
First, copy all the java source code for IList
hierarchy and its visitor, IListAlgo
, from the link http://www.owlnet.rice.edu/~comp212/01-fall/lectures/10/schemeFW/.
This link contains the package schemeFW.
Use the provided java classes as the starting point.
- Write a visitor called Length to compute the length of IList
using direct recursion and without using helpers. This visitor corresponds to the
method length() of scheme.AList .
- Write a visitor called GetMin to compute the minimum of IList,
assuming IList contains Integer objects. GetMin
should use a helper visitor called HelpGetMin to compute the minimum. This
corresponds to the getMin() method of scheme.AList as shown in
http://www.owlnet.rice.edu/~comp212/01-fall/labs/03/scheme/.
- 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 HelpGetSum.
D. X. Nguyen & S. B. Wong, Sept. 17, 2001.
dxnguyen@cs.rice.edu, swong@cs.rice.edu