设计模式 - 迭代器模式(iterator pattern) Java 迭代器(Iterator) 详解
来源:互联网 发布:java做计算器 编辑:程序博客网 时间:2024/05/16 13:00
迭代器模式(iterator pattern) Java 迭代器(Iterator) 详解
本文地址: http://blog.csdn.net/caroline_wendy
参考迭代器模式(iterator pattern): http://blog.csdn.net/caroline_wendy/article/details/35254643
Java的标准库(util)中包含迭代器接口(iterator interface), import java.util.Iterator;
继承(implements)迭代器接口(Iterator)需要重写(override)三个函数: hasNext(), next()和remove();
Java的聚合类型, 如ArrayList包含迭代器.
但是数组类型, 需要重写相应的迭代器(iterator).
具体方法:
1. ArrayList类型, 包含迭代器的方法, 可以直接返回.
/** * @time 2014年6月20日 */package iterator;import java.util.ArrayList;import java.util.Iterator;/** * @author C.L.Wang * */public class PancakeHouseMenu implements Menu {ArrayList<MenuItem> menuItems;/** * */public PancakeHouseMenu() {// TODO Auto-generated constructor stubmenuItems = new ArrayList<MenuItem>();addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99);addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99);addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49);addItem("Waffles","Waffles, with your choice of blueberries or strawberries", true, 3.59);}public void addItem(String name, String description,boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);menuItems.add(menuItem);}public Iterator<MenuItem> createIterator() {return menuItems.iterator();}}
2. 数组类型, 创建相应的迭代器类, 继承(implements)迭代器(Iterator), 重写迭代器的方法.
/** * @time 2014年6月26日 */package iterator;import java.util.Iterator;/** * @author C.L.Wang * */public class DinerMenu implements Menu {static final int MAX_ITEMS = 6;int numberOfItems = 0;MenuItem[] menuItems;/** * */public DinerMenu() {// TODO Auto-generated constructor stubmenuItems = new MenuItem[MAX_ITEMS];addItem("Vegetarian BLT", "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);addItem("BLT", "Bacon with lettuce & tomato on the whole wheat", false, 2.99);addItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29);addItem("Hotdog", "A hot dog, with saurkraut, relish, onions, topped with cheese", false, 3.05);}public void addItem(String name, String description,boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);if (numberOfItems >= MAX_ITEMS) {System.err.println("Sorry, menu is full! Can't add item to menu");} else {menuItems[numberOfItems] = menuItem;++numberOfItems;}}public Iterator<MenuItem> createIterator() {return new DinerMenuIterator(menuItems);}}/** * @time 2014年6月27日 */package iterator;import java.util.Iterator;/** * @author C.L.Wang * */public class DinerMenuIterator implements Iterator<MenuItem> {MenuItem[] items;int position = 0;/** * */public DinerMenuIterator(MenuItem[] items) {// TODO Auto-generated constructor stubthis.items = items;}/* (non-Javadoc) * @see iterator.Iterator#hasNext() */@Overridepublic boolean hasNext() {// TODO Auto-generated method stubif (position >= items.length || items[position] == null) {return false;}return true;}/* (non-Javadoc) * @see iterator.Iterator#next() */@Overridepublic MenuItem next() {// TODO Auto-generated method stubMenuItem menuItem = items[position];++position;return menuItem;}@Overridepublic void remove() {if (position <= 0) {throw new IllegalStateException("You can't remove an item until you've done at least one next()");}if (items[position-1] != null) {for (int i=position-1; i<(items.length-1); ++i) {items[i] = items[i+1];}items[items.length-1] = null;}}}
3. 菜单接口(interface), 包含创建迭代器(createIterator)的方法.
/** * @time 2014年6月27日 */package iterator;import java.util.Iterator;/** * @author C.L.Wang * */public interface Menu {public Iterator<MenuItem> createIterator();}
4. 具体的菜单项, ArrayList和数组的基本元素.
/** * @time 2014年6月20日 */package iterator;/** * @author C.L.Wang * */public class MenuItem {String name;String description;boolean vegetarian; //是否是素食double price;/** * */public MenuItem(String name, String description, boolean vegetarian, double price) {// TODO Auto-generated constructor stubthis.name = name;this.description = description;this.vegetarian = vegetarian;this.price = price;}public String getName() {return name;}public String getDescription() {return description;}public double getPrice() {return price;}public boolean isVegetarian() {return vegetarian;}}
5. 客户类(client), 调用迭代器(iterator)方法.
/** * @time 2014年6月27日 */package iterator;import java.util.Iterator;/** * @author C.L.Wang * */public class Waitress {Menu pancakeHouseMenu;Menu dinerMenu;/** * */public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {// TODO Auto-generated constructor stubthis.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;}public void printMenu() {Iterator<MenuItem> pancakeIterator = pancakeHouseMenu.createIterator();Iterator<MenuItem> dinerIterator = dinerMenu.createIterator();System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);}private void printMenu(Iterator<MenuItem> iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();System.out.print(menuItem.getName() + ": ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}}}
6. 测试:
/** * @time 2014年6月27日 */package iterator;/** * @author C.L.Wang * */public class MenuTestDrive {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();DinerMenu dinerMenu = new DinerMenu();Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);waitress.printMenu();}}
7. 输出:
MENU----BREAKFASTK&B's Pancake Breakfast: 2.99 -- Pancakes with scrambled eggs, and toastRegular Pancake Breakfast: 2.99 -- Pancakes with fried eggs, sausageBlueberry Pancakes: 3.49 -- Pancakes made with fresh blueberriesWaffles: 3.59 -- Waffles, with your choice of blueberries or strawberriesLUNCHVegetarian BLT: 2.99 -- (Fakin') Bacon with lettuce & tomato on whole wheatBLT: 2.99 -- Bacon with lettuce & tomato on the whole wheatSoup of the day: 3.29 -- Soup of the day, with a side of potato saladHotdog: 3.05 -- A hot dog, with saurkraut, relish, onions, topped with cheese
6 0
- 设计模式 - 迭代器模式(iterator pattern) Java 迭代器(Iterator) 详解
- 设计模式 - 迭代器模式(iterator pattern) 详解
- Java设计模式--迭代器模式【Iterator Pattern】
- Iterator Pattern--迭代器设计模式
- Java Iterator Pattern(迭代器模式)
- 设计模式 - 迭代器模式(iterator pattern) 扩展 详解
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 详解
- 设计模式21:Iterator Pattern (迭代器模式)
- 设计模式-迭代器模式(Iterator Pattern)
- c++设计模式:迭代器模式(Iterator Pattern)
- 设计模式-迭代器模式(iterator pattern)
- 设计模式之迭代器模式--- Pattern Iterator
- 设计模式【迭代器模式Iterator Pattern】
- 【设计模式】迭代器模式(Iterator Pattern)
- 迭代器模式(Iterator Pattern)
- 迭代器模式(Iterator pattern)
- 迭代器模式(Iterator Pattern)
- 迭代器模式(Iterator pattern)
- Android开发中处理图片OOM (OutOfMemoryError) 的若干方法小结
- 一种典型的Content Provider 代码架构
- 云安全三大趋势:纵深防御、软件定义安全、设备虚拟化
- 闲来看看View.java的Developer Guides
- IE浏览器 应用程序发生异常 未知的软件异常(0x0000417),位置为 0x6546120
- 设计模式 - 迭代器模式(iterator pattern) Java 迭代器(Iterator) 详解
- 翻页工具类
- OCP-1Z0-051-题目解析-第4题
- 关于CString字符类型
- Android开发之Frame动画实现方法
- 【KMP】 HDOJ 3336 Count the string
- javascript中鼠标事件
- 分布式搜索elasticsearch java API 之(六)------批量添加删除索引
- 黑马程序员——06多线程的实现方式与安全机制