设计模式 - 迭代器模式(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
- 设计模式 - 迭代器模式(iterator pattern) 扩展 详解
- 设计模式 - 迭代器模式(iterator pattern) 详解
- 设计模式 - 迭代器模式(iterator pattern) Java 迭代器(Iterator) 详解
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 详解
- Iterator Pattern--迭代器设计模式
- 设计模式21:Iterator Pattern (迭代器模式)
- 设计模式-迭代器模式(Iterator Pattern)
- c++设计模式:迭代器模式(Iterator Pattern)
- 设计模式-迭代器模式(iterator pattern)
- 设计模式之迭代器模式--- Pattern Iterator
- Java设计模式--迭代器模式【Iterator Pattern】
- 设计模式【迭代器模式Iterator Pattern】
- 【设计模式】迭代器模式(Iterator Pattern)
- 设计模式之迭代器(Iterator Pattern)-笔记
- Design Pattern Iterator 迭代器设计模式
- 设计模式之-迭代器(Iterator pattern)
- JS-Javascript 迭代器设计模式 Iterator pattern
- 迭代器模式(Iterator Pattern)
- STL(6)之Traits编程技法 与迭代器
- TortoiseGit之配置密钥
- 高级结果映射 ResultMap Association Collection
- MFC消息三部曲(2)——线程中控制窗口内容
- Java-集合--Java集合排序
- 设计模式 - 迭代器模式(iterator pattern) 扩展 详解
- 百度云离线下载含有违规内容检测方法分析
- python 学习之range()函数
- 在一个数组里实现不同的下标访问不一样的数据类型。
- 操作符重载和友元的关系
- wget从oracle网站中下载jdk
- C# struc的陷阱 (无法修改""的返回值,因为他不是变量)
- iOS 开发-理解Certificate、AppId和Provisioning Profile
- Starting application at 0x81000000 ...