迭代器模式:提供一种方法顺序来访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

设计一个打印菜单的系统有两种菜单:PancakeHouseMenu和DinerMenu,


写一个迭代器接口:

package cn.iterator.demo;

/**
 * 迭代器接口
 * 
 * @author wy
 * 
 */
public interface Iterator {

	public boolean hasNext();

	public Object next();
}
package cn.iterator.demo;

import java.util.ArrayList;

public class PancakeHouseMenu {

	private ArrayList menuItems;

	public PancakeHouseMenu() {
		menuItems = new ArrayList();
		addItem("K&B's breakfast", "eggs and toast", true, 2.99);
		addItem("Regular", "eggs and sausage", false, 3.00);
		addItem("BlueBerry", "berries", true, 2.88);
		addItem("Waffles", "waffles", false, 3.01);
	}

	public void addItem(String name, String desc, Boolean vagetarian,
			double price) {
		MenuItem menuItem = new MenuItem(name, desc, vagetarian, price);
		menuItems.add(menuItem);
	}

	public Iterator createIterator() {
		return new PancakeHouseIterator(menuItems);
	}
}
package cn.iterator.demo;

public class DinerMenu {

	private final static int MAX_ITEMS = 6;
	private int numberOfItems = 0;
	MenuItem[] menuItems;

	public DinerMenu() {
		menuItems = new MenuItem[MAX_ITEMS];
		addItem("BLT", "Fakin", true, 2.14);
		addItem("XX", "xxc", false, 2.87);
		addItem("YY", "yy", true, 6.5);
		addItem("ABC", "abc", true, 5.2);
	}

	public void addItem(String name, String desc, boolean vagetarian,
			double price) {
		MenuItem menuItem = new MenuItem(name, desc, vagetarian, price);
		if (numberOfItems >= MAX_ITEMS) {
			System.err.println("Sorry!the menu is full!");
		} else {
			menuItems[numberOfItems] = menuItem;
			numberOfItems++;
		}
	}

	/**
	 * 返回迭代器接口,客户不需要知道餐厅菜单是如何维护菜单项的,也不需要迭代器是如何实现的, 客户只需要直接使用这个迭代器来遍历菜单项即可
	 * 
	 * @return
	 */
	public Iterator createIterator() {
		return new DinnerMenuIterator(menuItems);
	}
}


package cn.iterator.demo;

public class MenuItem {

	private String name;
	private String desc;
	private boolean vagetarian;
	private double price;

	public MenuItem(String name, String desc, Boolean vagetarian, double price) {
		this.name = name;
		this.desc = desc;
		this.vagetarian = vagetarian;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public String getDesc() {
		return desc;
	}

	public boolean getVagetarian() {
		return vagetarian;
	}

	public double getPrice() {
		return price;
	}
}


上面是两个菜单类,接着写下两个菜单迭代器:

package cn.iterator.demo;

import java.util.ArrayList;

public class PancakeHouseIterator implements Iterator {

	private ArrayList<MenuItem> menuItems;

	private int position = 0;

	public PancakeHouseIterator(ArrayList<MenuItem> menuItems) {
		this.menuItems = menuItems;
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		if (position >= menuItems.size() || menuItems.get(position) == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Object next() {
		// TODO Auto-generated method stub
		return menuItems.get(position++);
	}

}
package cn.iterator.demo;

public class DinnerMenuIterator implements Iterator {

	private MenuItem[] menuItems;
	int position = 0;

	public DinnerMenuIterator(MenuItem[] menuItems) {
		this.menuItems = menuItems;
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		if (position >= menuItems.length || menuItems[position] == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Object next() {
		// TODO Auto-generated method stub
		return menuItems[position++];
	}

}

女服务员类:

package cn.iterator.demo;
/**
 * 女服务员类:由女服务员来打印菜单
 * @author wy
 *
 */
public class Waitress {

	PancakeHouseMenu pancakeHouseMenu;
	DinerMenu dinerMenu;

	public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {
		this.pancakeHouseMenu = pancakeHouseMenu;
		this.dinerMenu = dinerMenu;
	}

	public void printMenu() {
		Iterator panCakeHouseIterator = pancakeHouseMenu.createIterator();
		Iterator dinerMenuIterator = dinerMenu.createIterator();
		System.out.println("BreakFast Menu:");
		print(panCakeHouseIterator);
		System.out.println("Dinner Menu:");
		print(dinerMenuIterator);
	}

	private void print(Iterator it) {
		while (it.hasNext()) {
			MenuItem menuItem = (MenuItem) it.next();
			System.out.print("name:" + menuItem.getName());
			System.out.print("---desc:" + menuItem.getDesc());
			System.out.println("---price:" + menuItem.getPrice());
		}
		System.out.println();
	}
}
测试类:

package cn.iterator.demo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {

	public static void main(String[] args) {
		PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
		DinerMenu dinerMenu = new DinerMenu();

		pancakeHouseMenu.addItem("140106", "140106", true, 100);
		dinerMenu.addItem("140106", "140106", false, 99);

		Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);
		waitress.printMenu();
	}
}



posted on 2014-01-06 14:40  wyang0126  阅读(152)  评论(0编辑  收藏  举报