Comp201: Principles of Object-Oriented Programming I
Spring 2008 -- Lab 8   


Factories with Nested Classes and List Visitors using List Factories

For this lab, you may need to reference the lecture on factories. Additional information can be found in the Java Resources web pages on factories.

Exercise A: Factories with Nested Classes

Before you start, create a project in DrJava in the following manner:  

  1. Create a directory (folder) called Lab08.
  2. Create a subdirectory (subfolder) called lab8a.
  3. Create the src and classes folders in lab8a.
  4. Create a project in DrJava called carfac.pjt and save it in lab8a.
  5. Set the project build directory to the classes directory.

Consider the following model of cars and factories that make cars:

//--------------------------------------------------

/**
* Represents a car that can be driven.
*/
public interface ICar {
  /**
  * "Drives" the car, returning a string describing the how the car drives.
  */
  public abstract String drive();
}

//--------------------------------------------------

/**
* Represents a factory that makes cars
*/
public interface ICarFactory {
  /**
  * Returns an instance of a car
  */ 
  public abstract ICar makeCar();
}

//--------------------------------------------------

Write a test class that tests your factories. Be sure to use a variables of type ICarFactory and ICar rather than any concrete types! (Why?).

Your test code should show that the same code produces different results when one factory is used vs. another.

For fun, compile the following code and show that in the Interactions pane of DrJava, that you can randomly choose a car factory, have it make a car and drive it, getting the appropriate output every time! (This takes one line of code, once the 2 factories and the RandomChooser have been instantiated.)


public class RandomChooser {

  public static final RandomChooser Singleton = new RandomChooser();
  private RandomChooser() {
  }

  public Object choose(Object a, Object b) {
    return (Math.random()<0.5) ? a : b;
  }
}

Note that the above code is written generically for choosing any types of objects.   You will need to cast the return value to ICarFactory before calling the makeCar() method.

TO LABBIES: SKIP Exercise B below and GO TO Exercise C instead.

Exercise B: Factories with Anonymous Inner Classes

First, copy your entire lab8a folder and rename the copy "lab8b".   You can now open the carfac.pjt project in the new lab8b folder and work modify the code. Do not disturb the previous code in lab8a!

The syntax for your anonymous inner class should look something like:

new ICar() {
	public String drive() {
		...
	}
}

The way to "read" the above syntax is to say that we are "making a new instance of an ICar that has this drive() behavior".

Show that all your test code still works! This proves that the behavior of your system is independent of your implementation.

Solutions to Exercises A & B


Exercise C: Writing List Visitors using Abstract Factories.

Download and extract the list framework code from lecture 20Put all the visitors that you are to write below in the listFW.visitor package. Test your visitors using the CompositeListFactory factory class.

Caution:  Be sure that you are accessing the individual vararg parameter value rather than the entire vararg parameter array!   That is, params[0] vs params.

Problem 1:

Write a visitor called CopyFac to make a copy the host list .  CopyFac takes an IListFactory in its constructor (and this has a corresponding private field for such) and uses that factory to instantiate any needed lists.

Note: For your emptyCase(), should you return the host or _factory.makeEmptyList()? Think about what you want to mean by having the copy algorithm use a factory in the first place. Make your choice and add a comment to your code defending your choice.

Problem 2:

Write a visitor called ConcatFac to concatenate (i.e. append) the input list to the host list .  ConcatFac takes an IListFactory in its constructor (and this has a corresponding private field for such) and uses that factory to instantiate any needed lists.

Problem 3:

Write a visitor called ReverseFac to construct a list that is the reverse the host list .  ReverseFac takes an IListFactory in its constructor (and this has a corresponding private field for such) and uses that factory to instantiate any needed lists.  Hint: you may need to write a helper visitor here.  Your labbies will guide you through this problem.

Problem 4:

Write a visitor called RemoveLast to remove the last element of a list. As before, this visitor should take an IListFactory in its constructor. Hint: you may need to write a helper visitor here.  Your labbies will guide you through this problem.

 

 

 


Last Revised Thursday, 03-Jun-2010 09:50:27 CDT

©2008 Stephen Wong and Dung Nguyen