提供 一种方法 顺序访问 一个聚和 对象中的各个元素,而又不暴露其内部的表示或实现。
迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露其内部的表示。把游走的任务放在迭代器上,而不是聚合上。这样简化了聚合的接口和实现,也让责任各得其所。
1.迭代器允许访问聚合的元素,而不需要暴露它的内部结构。
2.迭代器将遍历聚合的工作封装进一个对象中。
3.当使用迭代器的时候,我们依赖聚合提供遍历。
4.迭代器提供了一个通用的接口,让我们遍历聚合项,当我们编码使用聚合的项时,就可以使用多太机制。
package pattern;
import java.util.ArrayList;
import java.util.Calendar;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
public class MenuTestDrive {
public static void main(String[] args) {
PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
DinerMenu dinerMenu = new DinerMenu();
CafeMenu cafeMenu = new CafeMenu();
Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu,cafeMenu);
waitress.printMenu();
}
}
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 name;
}
public String getDescription() {
return description;
}
public boolean isVegetarian() {
return vegetarian;
}
public double getPrice() {
return price;
}
}
interface Menu{
public Iterator creatIterator();
}
class PancakeHouseMenu implements Menu {
ArrayList<MenuItem> menuItems=new ArrayList<MenuItem>();
public PancakeHouseMenu() {
menuItems = 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,and sausage", true, 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 creatIterator() {
return new PancakeHouseMenuIterator(menuItems);
}
}
class DinerMenu implements Menu{
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem[] menuItems= new MenuItem[MAX_ITEMS];
public DinerMenu() {
addItem("Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat", true,
2.99);
addItem("BLT", " Bacon with lettuce & tomato on whole wheat", true,
2.99);
addItem("Soup of the day",
"Soup of the day,with a side of potato salad", true, 2.99);
addItem("Hotdog",
"A hot dog, with saurkraut,relish,onions,topped with cheese",
true, 2.99);
}
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 not add item to menu!");
} else {
menuItems[numberOfItems] = menuItem;
numberOfItems = numberOfItems + 1;
}
}
public Iterator creatIterator() {
return new DinerMenuIterator(menuItems);
}
}
class CafeMenu implements Menu{
Hashtable menuItems=new Hashtable();
int position=0;
public CafeMenu(){
addItem("Vegegie Burger and Air Fries",
"Veggie burger on a whole wheat bun,lettuce,tomato,and fries", true,
3.99);
addItem("Soup of the day", "A cup of the soup of the day,with a side salad", true,
2.99);
addItem("Burrito",
"A large burrito , with whole pinto beans,salsa,guacamole", true, 2.99);
}
public void addItem(String name, String description, boolean vegetarian,
double price) {
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
menuItems.put(position, menuItem);
position++;
}
public Iterator creatIterator() {
return new CafeMenuIterator(menuItems);
}
}
interface Iterator {
boolean hasNext();
Object next();
void remove(int i);
}
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;
} else
return true;
}
public Object next() {
MenuItem menuItem = items[position];
position = position + 1;
return menuItem;
}
public void remove(int i){
if(i>=items.length || items[i] == null){
System.err.println("err");
}else{
for(int n=i;n<items.length-1;n++){
items[n]=items[n+1];
}
}
}
}
class PancakeHouseMenuIterator implements Iterator {
ArrayList<MenuItem> items;
int position = 0;
public PancakeHouseMenuIterator(ArrayList<MenuItem> items) {
this.items = items;
}
public boolean hasNext() {
if (position >= items.size()) {
return false;
} else
return true;
}
public Object next() {
MenuItem menuItem = (MenuItem) items.get(position);
position = position + 1;
return menuItem;
}
@Override
public void remove(int i) {
items.remove(i);
}
}
class CafeMenuIterator implements Iterator {
Hashtable items;
int position = 0;
public CafeMenuIterator(Hashtable items) {
this.items = items;
}
public boolean hasNext() {
if (position >= items.size()) {
return false;
} else
return true;
}
public Object next() {
MenuItem menuItem = (MenuItem) items.get(position);
position++;
return menuItem;
}
@Override
public void remove(int i) {
items.remove(i);
}
}
class AlternatingDinerMenuIterator implements Iterator{
MenuItem[] items;
int position;
public AlternatingDinerMenuIterator(MenuItem[] items){
this.items=items;
Calendar rightNow=Calendar.getInstance();
position =rightNow.get(Calendar.DAY_OF_WEEK)%2;
}
public boolean hasNext(){
if(position>=items.length||items[position]==null){
return false;
}else{
return true;
}
}
public Object next(){
MenuItem menuItem=items[position];
position=position+2;
return menuItem;
}
public void remove(int i){
throw new UnsupportedOperationException("Alternating Diner Menu Iterator does not support remove()");
}
}
class Waitress {
Menu pancakeHouseMenu;
Menu dinerMenu;
CafeMenu cafeMenu;
public Waitress(Menu pancakeHouseMenu, Menu dinerMenu,CafeMenu cafeMenu) {
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinerMenu = dinerMenu;
this.cafeMenu= cafeMenu;
}
public void printMenu() {
Iterator PancakeHouseMenuIterator = pancakeHouseMenu
.creatIterator();
Iterator dinerMenuIterator = dinerMenu.creatIterator();
Iterator cafeMenuIterator=cafeMenu.creatIterator();
System.out.println("MENU\n------\nBREAKFAST");
printMenu(PancakeHouseMenuIterator);
System.out.println("\nLUNCH");
printMenu(dinerMenuIterator);
System.out.println("\nDINNER");
printMenu(cafeMenuIterator);
}
private void printMenu(Iterator iterator) {
while (iterator.hasNext()) {
MenuItem menuItem = (MenuItem) iterator.next();
System.out.print(menuItem.getName() + ",");
System.out.print(menuItem.getPrice() + "--");
System.out.println(menuItem.getDescription());
}
}
}