head first 设计模式 迭代模式笔记
public interface Iterator
{
MenuItem [] items;
int positions = 0;
publci DinerMenuIterator (MenuItem[] items)
{
this.items= items;
}
public Object next()
{
MenuItem menuItem = items[positions];
position = position + 1;
return menuItem ;
}
public boolean hasNext()
{
if(position >=items.length || items[position]==null)
{
return false;
}
else
{
return ture;
}
}
}
public class DinerMenu
{
static final int MAX_ITEMS = 6;
INT numberOfItems = 0;
MenuItem[] menuItems;
public Iterator createIterator()
{
return new DinerMenuIterator(menuItems);
}
}
public calss DinerMenuIterator
{
}
public calss PancakeHouseIterator
{
}
public class PancakeHouseMenu
{
static final int MAX_ITEMS = 6;
INT numberOfItems = 0;
MenuItem[] menuItems;
public Iterator createIterator()
{
return new PancakeHouseIterator(menuItems);
}
}
public class Waitress
{
PancakeHouseMenu pancakeHouseMenu;
DinerMenu dinerMenu;
public Waitress(PancakeHouseMenu PancakeHOuseMenu,DinerMenu dinerMenu)
{
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinerMenu = dinerMenu;
}
public void PrintMenu()
{
Iterator pancakeIterator = PancakeHouseMenu.createIterator();
Iterator dinerIterator = dinerMenu.createIterator();
System.out.println("MENU\n ----\n BREAKFAST");
printMenu(pancakeIterator);
System.out.println("\nLUNCH");
PrintMenu(dinerIterator);
}
private void PrintMenu(Iterator iterator)
{
while(iteraor.hasNext())
{
MenuItem menuItem = (MenuItem)iterator.next();
System.out.println(MenuItem.getName() + ", ");
System.out.println(MenuItem.getPrice() + ", ");
System.out.println(MenuItem.getDescription());
}
}
}