| CSC 207 | Algorithms and Object Oriented Design | Spring 2011 |
Summary: In this lab, you will get a concrete look at another example of the builder pattern.
/**
* Meal models a typical fast-food chain value meal consisting of an
* entree, a side, and a drink.
*/
public class Meal {
private String entree = ""; /** Primary dish of this meal */
private String side = ""; /** Side dish of this meal */
private String drink = ""; /** Drink for this meal */
/**
* Set the entree of this meal to the specified value.
*
* @param entree Specified value to set the entree of this meal to
*/
public void setEntree(String entree) {
this.entree = entree;
}
/**
* Set the side dish of this meal to the specified value.
*
* @param entree Specified value to set the side dish of this meal to
*/
public void setSide(String side) {
this.side = side;
}
/**
* Set the drink of this meal to the specified value.
*
* @param entree Specified value to set the drink of this meal to
*/
public void setDrink(String drink) {
this.drink = drink;
}
/**
* Return a string representation of this meal indicating all portions.
*
* @return a String indicating all portions this meal
*/
public String toString() {
return entree + " with a side of " + side + " and a " + drink;
}
}
Strings)
that need to be "built" in order to have a complete meal.
/** Abstract builder for the Meal objects, supporting all the
* operations for building a meal.
*
* @author YOUR NAME HERE
*/
public abstract class MealBuilder {
protected Meal theMeal;
// Additional methods here
}
Meal getMeal()void.
Should these methods be abstract or not?
/** Concrete builder for a meal with a burger, fries, and a cola. */
public class BurgerMealBuilder extends MealBuilder {
}
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".
HealthyMealBuilder that builds
a meal consisting of "Chicken Sandwich",
"Carrot Sticks", and "Diet Cola".
/** A cook that uses a builder for a meal to actually construct a meal
* and make it available.
*
* @author Jerod Weinman
* @author YOUR NAME HERE
*/
public class Cook {
/** A meal builder that specifies how to assemble a particular meal */
private MealBuilder builder;
/**
* Set the meal builder for this cook to the specified value.
*
* @param builder the meal builder for this cook to use
*/
public void setMealBuilder (MealBuilder builder) {
this.builder = builder;
}
/**
* Return the meal constructed by this cook.
*
* @return the meal constructed by this cook.
*/
public Meal getMeal() {
return builder.getMeal();
}
/**
* Uses the meal builder of this cook to construct all portions of a meal.
*/
public void constructMeal() {
// Take the necessary steps
}
}
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)
/** A driver class for building some value meals
*
* @author Jerod Weinman
*/
public class BuilderExample {
/**
* A program that creates a cook and uses it with various meal builders
* to construct meals.
*
* @param args unused command-line arguments
*/
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);
}
}
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