设计模式 - 迭代器模式(iterator pattern) 扩展 详解

来源:互联网 发布:软件著作权 专利 区别 编辑:程序博客网 时间:2024/06/05 00:09

迭代器模式(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 stubaddItem("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() */@Overridepublic Iterator<MenuItem> createIterator() {// TODO Auto-generated method stubreturn 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 stubthis.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 stubPancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();DinerMenu dinerMenu = new DinerMenu();CafeMenu cafeMenu = new CafeMenu();Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu);waitress.printMenu();}}

4. 输出:

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 cheeseDINNERSoup of the day: 3.69 -- A cup of the the soup of the day, with a side saladBurrito: 4.29 -- A large burrito, with whole pinto beans, salsa, quacamoleVeggie 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






6 0
原创粉丝点击