Laboratory: Building the Builder
CSC 207 Algorithms and Object Oriented Design Spring 2009

Summary: In this lab, you will get a concrete look at another example of the builder pattern.

Background

In class, we discussed that the builder pattern is used when some type of composite "product" class must be constructed. There may also be an abstract class representing the generic notion of the operations for building of a particular type of product. Various sub-classes are used for the particular products. It is the responsibility of a "director" to explicitly build all the components parts.

In this activity, we will return to the example of a fast food restaurant operation that builds your value meals (the product) on demand. Some fast food chains have pre-defined value meals that essentially let you, using an order-by-number scheme, choose predefined combinations of sandwich (or entree, more generically), fries (the side), and your soda (or drink).

Here is a class that models the product for us:

Meal.java
/**
 * Meal models a typical fast-food chain value meal consisting of an
 * entree, a side, and a drink. 
 */
public class Meal {
  
  private String entree = "";
  private String side = "";
  private String drink = "";

  public void setEntree(String entree) { 
    this.entree = entree;
  }

  public void setSide(String side) { 
    this.side = side;
  }

  public void setDrink(String drink) {
    this.drink = drink;
  }

  public String toString() {
    return entree + " with a side of " + side + " and a " + drink;
  }
}
Note that there are three objects (in this case, Strings) that need to be "built" in order to have a complete meal.

Exercises

Exercise 1

As mentioned above, a builder pattern requires an abstract builder class. In the following exercises, you will add functions to the following abstract meal builder.
MealBuilder.java
/** Abstract builder for the Meal objects, supporting all the
 *  operations for building a meal.
 */
public abstract class MealBuilder {

  protected Meal theMeal;

  // Additional methods here

}
  1. First of all, notice that the meal is not initialized. Add a method that creates an "empty" meal object. Should this method be abstract or not?
  2. Once a (concrete) builder finishes building the meal, we need some way to retrieve it. Add a function Meal getMeal() that allows someone to retrieve the value meal after a (more concrete) builder finishes building it. Should this method be abstract or not?
  3. Now, we need to specify what operations any meal builder must implement. These correspond to the objects that must be constructed, which in our case are the three elements of the meal.

    Add three methods that will allow a builder to "build" the three elements of the meal. Note: These functions should accept no arguments and return void.

    Should these methods be abstract or not?

Exercise 2

Our next step is to implement some concrete builders that actually put some food in your meal! Let's start with a typical fast food value meal.
BurgerMealBuilder.java
/** Concrete builder for a meal with a burger, fries, and a cola. */
public class BurgerMealBuilder extends MealBuilder {

}
  1. Add to BurgerMealBuilder the implementations of the three methods you defined in class MealBuilder that end up setting the entree to "Burger", the side to "Fries", and the drink to "Cola".
  2. Create a class called HealthyMealBuilder that builds a meal consisting of "Chicken Sandwich", "Carrot Sticks", and "Diet Cola".

Exercise 3

Our final step is the "director" that coordinates all the requisite action of the builders. Remember that a builder has methods that facilitate each step of the process, but it is the "director" (in our case a cook), that actually takes those steps!
Cook.java
/** A cook that uses a builder for a meal to actually construct a meal
 * and make it available.
 */
public class Cook {

  private MealBuilder builder;

  public void setMealBuilder (MealBuilder builder) {
    this.builder = builder;
  }

  public Meal getMeal() {
    return builder.getMeal();
  }

  public void constructMeal() {
    // Take the necessary steps
  }

}
  1. Using the methods available in a MealBuilder object, add code to the constructMeal method above that (finally!) constructs the meal. You should have four function calls! (Don't forget about 1.a)

Exercise 4

If you have time, you should be able to test your fast food restaurant with the following code.
BuilderExample.java
/** A driver class for building some value meals */
public class BuilderExample {
  public static void main(String[] args) {

    Cook cook = new Cook();
    Meal meal;

    BurgerMealBuilder burgerBuilder = new BurgerMealBuilder();
    HealthyMealBuilder healthyBuilder = new HealthyMealBuilder();

    cook.setMealBuilder (burgerBuilder);

    cook.constructMeal();
    meal = cook.getMeal();

    System.out.println("Order up! A " + meal);



    cook.setMealBuilder (healthyBuilder);

    cook.constructMeal();
    meal = cook.getMeal();

    System.out.println("Order up! A " + meal);
  }
}
Which should produce
Order up! A Burger with a side of Fries and a Cola
Order up! A Chicken Sandwich with a side of Carrot Sticks and a Diet Cola
Jerod Weinman
Created: 6 January 2009