10.迭代器模式
Posted Kyhoon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了10.迭代器模式相关的知识,希望对你有一定的参考价值。
迭代器模式简单的说就是将含有相同类型的"集合"但不同"集合"类型的两类甚至更多类融合在一起进行遍历 .
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接口
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方法的接口)
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.客户程序
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.应用
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(); } }
以上是关于10.迭代器模式的主要内容,如果未能解决你的问题,请参考以下文章
设计模式迭代器模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )