Short exercise for intro to design patterns

Please do the following exercises for Monday. The syllabus indicates that short exercises should be turned in by email, but since these involve drawing diagrams, they should be done on paper. Please turn it in to me either in class on Monday or in my box.

Consider the following interfaces and classes for modeling orders at a sandwich shop.

import java.util.HashSet;

interface Bread {}

class White implements Bread {}
class Wheat implements Bread {}
class Rye implements Bread {}

interface Ingredient {}

class Turkey implements Ingredient {}
class Ham implements Ingredient {}
class Cheese implements Ingredient {}
class Lettuce implements Ingredient {}

class Sandwich {
    private Bread bread;
    private HashSet ingredients;

    public Sandwich(Bread bread) {
        this.bread = bread;
    }

    public void addIngredient(Ingredient added) {
        ingredients.add(added);
    }
}

interface Side {}

class FrenchFries implements Side {}
class OnionRings implements Side {}
class CheeseSticks implements Side{}

abstract class ValueMeal {
    protected Sandwich sandwich;
    protected HashSet sides;
}


// double turkey and cheese on wheat with a side of onion rings
class FridaySpecial extends ValueMeal {
    public FridaySpecial() {
        sandwich = new Sandwich(new Wheat());
        sandwich.addIngredient(new Turkey());
        sandwich.addIngredient(new Turkey());
        sandwich.addIngredient(new Cheese());
        sides = new HashSet();
        sides.add(new OnionRings());
    }
}

1. The owner tells you that the ValueMeal class needs to have an operation extraValue(), something which will "supersize" the order. Whenever a value meal is supersized, one ingredient and one side is added. However, which ingredient and which side to add is dependent on the type of value meal. Name the pattern we should use and draw a UML diagram.

2. Some of the shop's regular customers are a little weird and like to have their sides inside their sandwich---for example, one fellow wants onion rings as an ingredient, not a side. Without changing any of the existing classes or interfaces, make it so a side can be treated as an ingredient. Name the pattern we should use and draw a UML diagram.


Last modified: Wed Jan 21 17:29:58 CST 2009