Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu.createIterator is one-shot iterator only #25

Open
JerryYangSH opened this issue Nov 21, 2019 · 0 comments
Open

Menu.createIterator is one-shot iterator only #25

JerryYangSH opened this issue Nov 21, 2019 · 0 comments

Comments

@JerryYangSH
Copy link

JerryYangSH commented Nov 21, 2019

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant