迭代器模式(iterator pattern) 扩展 详解
本文地址: http://blog.csdn.net/caroline_wendy
参考迭代器模式-Java迭代器: http://blog.csdn.net/caroline_wendy/article/details/35268931
扩展迭代器模式, 添加一个Hashtable存储的类.
具体方法:
1. Hashtable的类, 包含创建value()的迭代器(iterator).
/** * @time 2014年6月27日 */ package iterator; import java.util.Hashtable; import java.util.Iterator; /** * @author C.L.Wang * */ public class CafeMenu implements Menu { Hashtable<String, MenuItem> menuItems = new Hashtable<String, MenuItem>(); /** * */ public CafeMenu() { // TODO Auto-generated constructor stub 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 the soup of the day, with a side salad", false, 3.69); addItem("Burrito", "A large burrito, with whole pinto beans, salsa, quacamole", 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); } /* (non-Javadoc) * @see iterator.Menu#createIterator() */ @Override public Iterator<MenuItem> createIterator() { // TODO Auto-generated method stub return menuItems.values().iterator(); } }
2. 客户类(client), 调用迭代器(Iterator).
/** * @time 2014年6月27日 */ package iterator; import java.util.Iterator; /** * @author C.L.Wang * */ public class Waitress { Menu pancakeHouseMenu; Menu dinerMenu; Menu cafeMenu; /** * */ public Waitress(Menu pancakeHouseMenu, Menu dinerMenu, Menu cafeMenu) { // TODO Auto-generated constructor stub this.pancakeHouseMenu = pancakeHouseMenu; this.dinerMenu = dinerMenu; this.cafeMenu = cafeMenu; } public void printMenu() { Iterator<MenuItem> pancakeIterator = pancakeHouseMenu.createIterator(); Iterator<MenuItem> dinerIterator = dinerMenu.createIterator(); Iterator<MenuItem> cafeIterator = cafeMenu.createIterator(); System.out.println("MENU\n----\nBREAKFAST"); printMenu(pancakeIterator); System.out.println("\nLUNCH"); printMenu(dinerIterator); System.out.println("\nDINNER"); printMenu(cafeIterator); } 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()); } } }
3. 测试:
/** * @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 stub PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); DinerMenu dinerMenu = new DinerMenu(); CafeMenu cafeMenu = new CafeMenu(); Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu); waitress.printMenu(); } }
4. 输出:
MENU ---- BREAKFAST 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 made with fresh blueberries Waffles: 3.59 -- Waffles, with your choice of blueberries or strawberries LUNCH Vegetarian BLT: 2.99 -- (Fakin‘) Bacon with lettuce & tomato on whole wheat BLT: 2.99 -- Bacon with lettuce & tomato on the 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 DINNER Soup of the day: 3.69 -- A cup of the the soup of the day, with a side salad Burrito: 4.29 -- A large burrito, with whole pinto beans, salsa, quacamole Veggie Burger and Air Fries: 3.99 -- Veggie burger on a whole wheat bun, lettuce, tomato, and fries
5. 其余代码参考: http://blog.csdn.net/caroline_wendy/article/details/35268931
设计模式 - 迭代器模式(iterator pattern) 扩展 详解,布布扣,bubuko.com
时间: 2024-10-05 04:19:15