Skip to content

Menu.createIterator is one-shot iterator only #25

Description

@JerryYangSH

The Menu.createIterator is one-shot iterator, not support re-iterating. This looks a bug to me. Could you have a chance to fix this?

public class Menu extends MenuComponent {
    private Iterator iterator; // Seriously!
    private List<MenuComponent> menuComponents;
...
    public Iterator<MenuComponent> createIterator() {
		if (iterator == null) {
			iterator = new CompositeIterator(menuComponents.iterator());
		}
		return iterator; // the `iterator' never resets to null once it's set.
	}
...
}

...
headfirst.designpatterns.composite.menuiterator
public class MenuTestDrive {
	public static void main(String args[]) {
...
		Waitress waitress = new Waitress(allMenus);
   
		waitress.printVegetarianMenu();
		waitress.printVegetarianMenu(); // calling printVegetarianMenu again will print nothing
	}

Output is : 

VEGETARIAN MENU
----
  K&B's Pancake Breakfast(v), 2.99
     -- Pancakes with scrambled eggs, and toast
  Blueberry Pancakes(v), 3.49
     -- Pancakes made with fresh blueberries, and blueberry syrup
  Waffles(v), 3.59
     -- Waffles, with your choice of blueberries or strawberries
  Vegetarian BLT(v), 2.99
     -- (Fakin') Bacon with lettuce & tomato on whole wheat
  Steamed Veggies and Brown Rice(v), 3.99
     -- A medly of steamed vegetables over brown rice
  Pasta(v), 3.89
     -- Spaghetti with Marinara Sauce, and a slice of sourdough bread
  Apple Pie(v), 1.59
     -- Apple pie with a flakey crust, topped with vanilla icecream
  Cheesecake(v), 1.99
     -- Creamy New York cheesecake, with a chocolate graham crust
  Sorbet(v), 1.89
     -- A scoop of raspberry and a scoop of lime
  Veggie Burger and Air Fries(v), 3.99
     -- Veggie burger on a whole wheat bun, lettuce, tomato, and fries
  Burrito(v), 4.29
     -- A large burrito, with whole pinto beans, salsa, guacamole

VEGETARIAN MENU
----
Process finished with exit code 0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions