import java.util.Iterator;
public interface Menu {
public Iterator createIterator();
}
import java.util.Iterator;
publicclass DinerMenu implements Menu{
privatestaticfinalint MAX_ITEMS=6;
privateint numberOfItems=0;
private MenuItem[] menuItems;
public DinerMenu()
{
menuItems=new MenuItem[MAX_ITEMS];
addItem("Dinner Vegetarian","Bacon",true,2.99);
addItem("Dinner BLT ","Bacon BLT",false,2.99);
addItem("Dinner hotdog","A hot dog",false,3.48);
}
publicvoid addItem(String name,String description,boolean vegetarian,double price)
{
MenuItem item=new MenuItem(name,description,vegetarian,price);
if(numberOfItems<MAX_ITEMS)
{
menuItems[numberOfItems]=item;
numberOfItems++;
}
else
System.out.println("Sorry ,menu is full");
}
public Iterator createIterator()
{
return new DinerMenuIterator(menuItems);
}
}
import java.util.ArrayList;
import java.util.Iterator;
publicclass PancakeHouseMenu implements Menu{
private ArrayList menuItems;
public PancakeHouseMenu()
{
menuItems=new ArrayList();
addItem("K&B panckage breakfast","pancakes with scramed eggs and toast",true,2.99);
addItem("Regular panckage breakfast","pancakes with fried eggs ",false,2.99);
addItem("Blueberry breakfast","pancakes made with fresh blueberry ",true,3.48);
addItem("Waffles","waffles, with your choice",true,3.59);
}
publicvoid addItem(String name,String description,boolean vegetarian,double price)
{
MenuItem item=new MenuItem(name,description,vegetarian,price);
menuItems.add(item);
}
public Iterator createIterator()
{
return menuItems.iterator();
}
}
import java.util.HashMap;
import java.util.Iterator;
publicclass CoffeeMenu implements Menu{
private HashMap menuItems=new HashMap();
public CoffeeMenu()
{
addItem("Coffee Burger" ,"veggie burger on",true,3.99);
addItem("Coffee Soup","A cup of the soup",false,3.69);
addItem("Coffee Burrito","A large burrito",true,4.29);
}
publicvoid addItem(String name,String description,boolean vegetarian,double price)
{
MenuItem item=new MenuItem(name,description,vegetarian,price);
menuItems.put(item.getName(),item);
}
@Override
public Iterator createIterator() {
// TODO Auto-generated method stub
//直接返回一个迭代器
return menuItems.values().iterator();
}
}
import java.util.Iterator;
publicclass DinerMenuIterator implements Iterator {
private MenuItem[] items;
privateint position=0;//存放当前遍历的坐标
public DinerMenuIterator(MenuItem[] items)
{
this.items=items;
}
public boolean hasNext() {
if(position>=items.length || items[position]==null)
return false;
else
return true;
}
public Object next() {
MenuItem menuItem=items[position];
position=position+1;
return menuItem;
}
public void remove() {
if(position<=0)
throw new IllegalStateException("You can't remove an item at least");
if(items[position]!=null)
{
for(int i=position-1;i<items.length-1;i++)
items[i]=items[i+1];
items[items.length-1]=null;
}
}
}
import java.util.Iterator;
public class Waitress {
private Menu diner;
private Menu house;
private Menu cafe;
public Waitress(Menu diner,Menu house,Menu cafe)
{
this.diner=diner;
this.house=house;
this.cafe=cafe;
}
publicvoid printMenu()
{
Iterator houseIterator=house.createIterator();
Iterator dinerIterator=diner.createIterator();
Iterator cafeIterator=cafe.createIterator();
System.out.println("-------houseMenu--------");
printMenuItem(houseIterator);
System.out.println();
System.out.println("-------dinnerMenu--------");
printMenuItem(dinerIterator);
System.out.println();
System.out.println("-------coffeeMenu--------");
printMenuItem(cafeIterator);
}
private void printMenuItem(Iterator iterator)
{
while(iterator.hasNext())
{
MenuItem menuItem=(MenuItem)iterator.next();
System.out.print(menuItem.getName()+"_||_"+menuItem.getPrice()
+"_||_"+menuItem.getDescription());
System.out.println();
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
//组合需要和叶子节点一样实现相同的接口
publicclass Flock implements Quackable {
private ArrayList quackers=new ArrayList();
publicvoid add(Quackable quacker)
{
quackers.add(quacker);
}
//通过实现相同的接口,就能保证每次循环遍历完所有的单个对象和对象集合
@Override
publicvoid quack() {
// TODO Auto-generated method stub
Iterator iterator=quackers.iterator();
while(iterator.hasNext())
{
Quackable quacker=(Quackable) iterator.next();
quacker.quack();//注意这里
}
}
}