迭代器模式簡單的說就是將含有相同類型的"集合"但不一樣"集合"類型的兩類甚至更多類融合在一塊兒進行遍歷 .java
1.含有的相同類型(這裏以菜單爲例)數組
public class MenuItem { String name; String description; boolean vegetarian; double price; public MenuItem(String name,String description,boolean vegetarian,double price){ this.name=name; this.description=description; this.vegetarian=vegetarian; this.price=price; } public String getName(){ return this.name; } public String getDescription(){ return this.description; } public double getPrice(){ return this.price; } public boolean isVegetarian(){ return this.vegetarian; } }
2.將兩種不一樣方式,統一繼承Iterator接口this
public class DinerMenuIterator implements Iterator { MenuItem[] items; int position=0; public DinerMenuIterator(MenuItem[] items){ this.items=items; } public boolean hasNext() { if(position>=items.length || items[position]==null){ return false; } return true; } public Object next() { MenuItem menuItem=items[position]; position++; return menuItem; } public void remove() { // TODO Auto-generated method stub } } public class PancakeHouseIterator implements Iterator{ ArrayList<MenuItem> items; int position=0; public PancakeHouseIterator(ArrayList<MenuItem> items){ this.items=items; } public boolean hasNext() { if(position>=items.size()){ return false; } return true; } public Object next() { return items.get(position++); } public void remove() { // TODO Auto-generated method stub } }
3.存儲「集合」的不一樣方式(其實能夠統一繼承一個含有createIterator方法的接口)blog
1) 數組的存儲方式繼承
public class ArrayItem { private MenuItem[] items; private int itemCount; public ArrayItem(int count){ items=new MenuItem[count]; itemCount = 0; } public void addItem(String name,String des,boolean vege,double price){ MenuItem item=new MenuItem(name, des, vege, price); items[itemCount] = item; this.itemCount++; } public Iterator createIterator(){ return new DinerMenuIterator(items); } }
2)list的存儲方式接口
public class ListItem { private ArrayList<MenuItem> items; public ListItem(){ items=new ArrayList(); } public void addItem(String name,String des,boolean vege,double price){ MenuItem item=new MenuItem(name, des, vege, price); items.add(item); } public Iterator createIterator(){ return new PancakeHouseIterator(items); } }
4.客戶程序ip
public class Waitress { ListItem pancakeHouseMenu; ArrayItem dinerMenu; public Waitress(ListItem pancakeHouseMenu,ArrayItem dinerMenu){ this.pancakeHouseMenu=pancakeHouseMenu; this.dinerMenu=dinerMenu; } public void printMenu(){ Iterator pancakeHouseIte=this.pancakeHouseMenu.createIterator(); Iterator dinerIte =this.dinerMenu.createIterator(); printMenu(pancakeHouseIte); printMenu(dinerIte); } private void printMenu(Iterator iterator){ while(iterator.hasNext()){ MenuItem item = (MenuItem)iterator.next(); System.out.println(item.getName()+":"+item.getDescription()+":"+item.getPrice()); } } }
5.應用rem
public class App { public static void main(String[] args) { ListItem listItem=new ListItem(); listItem.addItem("list", "list", true, 22); ArrayItem arrayItem=new ArrayItem(6); arrayItem.addItem("array", "array", false, 33); Waitress waitress=new Waitress(listItem, arrayItem); waitress.printMenu(); } }