定义迭代器模式
迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。
实例:
三个餐厅要合并,每个餐厅的菜单采用不同和数据结构实现,服务员需要获取所有的菜单信息,但是处理逻辑又不能过于复杂。
/**
* 菜单项
*/
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 double getPrice() {
return price;
}
public boolean isVegetarian() {
return vegetarian;
}
public String getDescription() {
return description;
}
public String getName() {
return name;
}
}
/**
* 菜单接口
*/
public interface Menu {
Iterator<MenuItem> createIterator();
}
/**
* 午餐菜单
*/
public class DinerMenu implements Menu{
@Override
public Iterator<MenuItem> createIterator() {
return new DinerMenuIterator(menuItems);
}
static final int MAX_ITEMS=6;
int numberOfItems=0;
MenuItem[] menuItems;
public DinerMenu(){
menuItems=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 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 vegerarian,double price){
MenuItem menuItem=new MenuItem(name,description,vegerarian,price);
if(numberOfItems>=MAX_ITEMS){
System.out.println("Sorry,menu is full! Can't add item to menu");
}else{
menuItems[numberOfItems]=menuItem;
numberOfItems++;
}
}
}
/**
* 早餐菜单
*/
public class PancakeHouseMenu implements Menu{
@Override
public Iterator<MenuItem> createIterator() {
return menuItems.iterator();
}
ArrayList<MenuItem> menuItems;
public PancakeHouseMenu(){
menuItems=new ArrayList<>();
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 make 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 class CafeMenu implements Menu{
@Override
public Iterator<MenuItem> createIterator() {
return menuItems.values().iterator();
}
Hashtable<String,MenuItem> menuItems=new Hashtable<>();
public CafeMenu(){
addItem("Veggie 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",false,3.69);
addItem("Burrito","A large burrito ,with whole pinto beans,salsa,guacamole",true,4.29);
}
public void addItem(String name,String description,boolean vegetarian,double price){
MenuItem menuItem=new MenuItem(name,description,vegetarian,price);
menuItems.put(menuItem.getName(),menuItem);
}
}
/**
* 迭代器具体实现类,用于遍历午餐
*/
public class DinerMenuIterator implements Iterator<MenuItem>{
private MenuItem[] menuItems;
private int position=0;
public DinerMenuIterator(MenuItem[] menuItems){
this.menuItems=menuItems;
}
@Override
public boolean hasNext() {
if(position>=menuItems.length||menuItems[position]==null){
return false;
}else {
return true;
}
}
@Override
public MenuItem next() {
MenuItem menuItem=menuItems[position];
position++;
return menuItem;
}
}
/**
* 服务员类
*/
public class Waitress {
ArrayList<Menu> menus;
public Waitress(ArrayList<Menu> menus){
this.menus=menus;
}
public void printMenu(){
Iterator<Menu> menuItemIterator=menus.iterator();
while(menuItemIterator.hasNext()){
Menu menu=menuItemIterator.next();
printMenu(menu.createIterator());
}
}
private void printMenu(Iterator<MenuItem> iterator){
System.out.println();
while (iterator.hasNext()){
MenuItem menuItem=iterator.next();
System.out.print(menuItem.getName()+", ");
System.out.print(menuItem.getPrice()+" -- " );
System.out.println(menuItem.getDescription());
}
System.out.println();
}
}
测试:
public class Test {
public static void main(String[] args){
Menu dinerMenu=new DinerMenu();
Menu pancakeMenu=new PancakeHouseMenu();
Menu cafeMenu=new CafeMenu();
ArrayList<Menu> menus=new ArrayList<>();
menus.add(dinerMenu);
menus.add(pancakeMenu);
menus.add(cafeMenu);
Waitress waitress=new Waitress(menus);
waitress.printMenu();
}
}
Vegetarian BLT, 2.99 -- Fakin Bacon with lettuce & tomato on whole wheat
BLT, 2.99 -- Bacon with lettuce & tomato on whole wheat
Soup of the day, 3.29 -- Soup of the day,with a side of potato salad
Hotdog, 3.05 -- A hot dog,with saurkraut,relish,onions,topped with cheese
K&B's Pancake Breakfast, 2.99 -- Pancakes with scrambled eggs, and toast
Regular Pancake Breakfast, 2.99 -- Pancakes with fried eggs,sausage
Blueberry Pancakes, 3.49 -- Pancakes make with fresh blueberries
Waffles, 3.59 -- Waffles,with your choice of blueberries or strawberries
Soup of the day, 3.69 -- A cup of the soup of the day, with a side salad
Burrito, 4.29 -- A large burrito ,with whole pinto beans,salsa,guacamole
Veggie Burger and Air Fries, 3.99 -- Veggie burger on a whole wheat bun, lettuce,tomato,and fries