HeadFirst设计模式之迭代器模式
一、
1.迭代器模式是对遍历集合元素的抽象
2.The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
3.
二、
1.
package headfirst.designpatterns.iterator.dinermerger;public interface Iterator {boolean hasNext();MenuItem next(); }
2.
1 package headfirst.designpatterns.iterator.dinermerger; 2 3 public class DinerMenuIterator implements Iterator { 4 MenuItem[] items; 5 int position = 0; 6 7 public DinerMenuIterator(MenuItem[] items) { 8 this.items = items; 9 } 10 11 public MenuItem next() { 12 MenuItem menuItem = items[position]; 13 position = position + 1; 14 return menuItem; 15 } 16 17 public boolean hasNext() { 18 if (position >= items.length || items[position] == null) { 19 return false; 20 } else { 21 return true; 22 } 23 } 24 }
3.
1 package headfirst.designpatterns.iterator.dinermerger; 2 3 import java.util.ArrayList; 4 5 public class PancakeHouseMenuIterator implements Iterator { 6 ArrayList<MenuItem> items; 7 int position = 0; 8 9 public PancakeHouseMenuIterator(ArrayList<MenuItem> items) { 10 this.items = items; 11 } 12 13 public MenuItem next() { 14 MenuItem item = items.get(position); 15 position = position + 1; 16 return item; 17 } 18 19 public boolean hasNext() { 20 if (position >= items.size()) { 21 return false; 22 } else { 23 return true; 24 } 25 } 26 }
4.
1 package headfirst.designpatterns.iterator.dinermerger; 2 3 public class DinerMenu implements Menu { 4 static final int MAX_ITEMS = 6; 5 int numberOfItems = 0; 6 MenuItem[] menuItems; 7 8 public DinerMenu() { 9 menuItems = new MenuItem[MAX_ITEMS]; 10 11 addItem("Vegetarian BLT", 12 "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99); 13 addItem("BLT", 14 "Bacon with lettuce & tomato on whole wheat", false, 2.99); 15 addItem("Soup of the day", 16 "Soup of the day, with a side of potato salad", false, 3.29); 17 addItem("Hotdog", 18 "A hot dog, with saurkraut, relish, onions, topped with cheese", 19 false, 3.05); 20 addItem("Steamed Veggies and Brown Rice", 21 "Steamed vegetables over brown rice", true, 3.99); 22 addItem("Pasta", 23 "Spaghetti with Marinara Sauce, and a slice of sourdough bread", 24 true, 3.89); 25 } 26 27 public void addItem(String name, String description, 28 boolean vegetarian, double price) 29 { 30 MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 31 if (numberOfItems >= MAX_ITEMS) { 32 System.err.println("Sorry, menu is full! Can't add item to menu"); 33 } else { 34 menuItems[numberOfItems] = menuItem; 35 numberOfItems = numberOfItems + 1; 36 } 37 } 38 39 public MenuItem[] getMenuItems() { 40 return menuItems; 41 } 42 43 public Iterator createIterator() { 44 return new DinerMenuIterator(menuItems); 45 // To test Alternating menu items, comment out above line, 46 // and uncomment the line below. 47 //return new AlternatingDinerMenuIterator(menuItems); 48 } 49 50 // other menu methods here 51 }
5.
1 package headfirst.designpatterns.iterator.dinermerger; 2 3 import java.util.ArrayList; 4 5 public class PancakeHouseMenu implements Menu { 6 ArrayList<MenuItem> menuItems; 7 8 public PancakeHouseMenu() { 9 menuItems = new ArrayList<MenuItem>(); 10 11 addItem("K&B's Pancake Breakfast", 12 "Pancakes with scrambled eggs, and toast", 13 true, 14 2.99); 15 16 addItem("Regular Pancake Breakfast", 17 "Pancakes with fried eggs, sausage", 18 false, 19 2.99); 20 21 addItem("Blueberry Pancakes", 22 "Pancakes made with fresh blueberries", 23 true, 24 3.49); 25 26 addItem("Waffles", 27 "Waffles, with your choice of blueberries or strawberries", 28 true, 29 3.59); 30 } 31 32 public void addItem(String name, String description, 33 boolean vegetarian, double price) 34 { 35 MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 36 menuItems.add(menuItem); 37 } 38 39 public ArrayList<MenuItem> getMenuItems() { 40 return menuItems; 41 } 42 43 public Iterator createIterator() { 44 return new PancakeHouseMenuIterator(menuItems); 45 } 46 47 public String toString() { 48 return "Objectville Pancake House Menu"; 49 } 50 51 // other menu methods here 52 }
6.
1 package headfirst.designpatterns.iterator.dinermerger; 2 3 public interface Menu { 4 public Iterator createIterator(); 5 }
7.
1 package headfirst.designpatterns.iterator.dinermerger; 2 3 public class MenuItem { 4 String name; 5 String description; 6 boolean vegetarian; 7 double price; 8 9 public MenuItem(String name, 10 String description, 11 boolean vegetarian, 12 double price) 13 { 14 this.name = name; 15 this.description = description; 16 this.vegetarian = vegetarian; 17 this.price = price; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public String getDescription() { 25 return description; 26 } 27 28 public double getPrice() { 29 return price; 30 } 31 32 public boolean isVegetarian() { 33 return vegetarian; 34 } 35 public String toString() { 36 return (name + ", $" + price + "\n " + description); 37 } 38 }
8.
1 package headfirst.designpatterns.iterator.dinermerger; 2 3 public class Waitress { 4 Menu pancakeHouseMenu; 5 Menu dinerMenu; 6 7 public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) { 8 this.pancakeHouseMenu = pancakeHouseMenu; 9 this.dinerMenu = dinerMenu; 10 } 11 12 public void printMenu() { 13 Iterator pancakeIterator = pancakeHouseMenu.createIterator(); 14 Iterator dinerIterator = dinerMenu.createIterator(); 15 16 System.out.println("MENU\n----\nBREAKFAST"); 17 printMenu(pancakeIterator); 18 System.out.println("\nLUNCH"); 19 printMenu(dinerIterator); 20 21 } 22 23 private void printMenu(Iterator iterator) { 24 while (iterator.hasNext()) { 25 MenuItem menuItem = iterator.next(); 26 System.out.print(menuItem.getName() + ", "); 27 System.out.print(menuItem.getPrice() + " -- "); 28 System.out.println(menuItem.getDescription()); 29 } 30 } 31 32 public void printVegetarianMenu() { 33 printVegetarianMenu(pancakeHouseMenu.createIterator()); 34 printVegetarianMenu(dinerMenu.createIterator()); 35 } 36 37 public boolean isItemVegetarian(String name) { 38 Iterator breakfastIterator = pancakeHouseMenu.createIterator(); 39 if (isVegetarian(name, breakfastIterator)) { 40 return true; 41 } 42 Iterator dinnerIterator = dinerMenu.createIterator(); 43 if (isVegetarian(name, dinnerIterator)) { 44 return true; 45 } 46 return false; 47 } 48 49 50 private void printVegetarianMenu(Iterator iterator) { 51 while (iterator.hasNext()) { 52 MenuItem menuItem = iterator.next(); 53 if (menuItem.isVegetarian()) { 54 System.out.print(menuItem.getName()); 55 System.out.println("\t\t" + menuItem.getPrice()); 56 System.out.println("\t" + menuItem.getDescription()); 57 } 58 } 59 } 60 61 private boolean isVegetarian(String name, Iterator iterator) { 62 while (iterator.hasNext()) { 63 MenuItem menuItem = iterator.next(); 64 if (menuItem.getName().equals(name)) { 65 if (menuItem.isVegetarian()) { 66 return true; 67 } 68 } 69 } 70 return false; 71 } 72 }
9.
1 package headfirst.designpatterns.iterator.dinermerger; 2 3 import java.util.*; 4 5 public class MenuTestDrive { 6 public static void main(String args[]) { 7 PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); 8 DinerMenu dinerMenu = new DinerMenu(); 9 10 Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu); 11 12 // Without iterators 13 //printMenu(); 14 15 // With iterators 16 waitress.printMenu(); 17 18 } 19 20 /* 21 * Without the Waitress, we need the code below... 22 */ 23 public static void printMenu() { 24 PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); 25 DinerMenu dinerMenu = new DinerMenu(); 26 27 ArrayList<MenuItem> breakfastItems = pancakeHouseMenu.getMenuItems(); 28 MenuItem[] lunchItems = dinerMenu.getMenuItems(); 29 30 // Hiding implementation 31 System.out.println("USING FOR EACH"); 32 for (MenuItem menuItem : breakfastItems) { 33 System.out.print(menuItem.getName()); 34 System.out.println("\t\t" + menuItem.getPrice()); 35 System.out.println("\t" + menuItem.getDescription()); 36 } 37 for (MenuItem menuItem : lunchItems) { 38 System.out.print(menuItem.getName()); 39 System.out.println("\t\t" + menuItem.getPrice()); 40 System.out.println("\t" + menuItem.getDescription()); 41 } 42 43 // Exposing implementation 44 System.out.println("USING FOR LOOPS"); 45 for (int i = 0; i < breakfastItems.size(); i++) { 46 MenuItem menuItem = (MenuItem)breakfastItems.get(i); 47 System.out.print(menuItem.getName()); 48 System.out.println("\t\t" + menuItem.getPrice()); 49 System.out.println("\t" + menuItem.getDescription()); 50 } 51 52 for (int i = 0; i < lunchItems.length; i++) { 53 MenuItem menuItem = lunchItems[i]; 54 System.out.print(menuItem.getName()); 55 System.out.println("\t\t" + menuItem.getPrice()); 56 System.out.println("\t" + menuItem.getDescription()); 57 } 58 } 59 }
三、用java.util.Iterator
1.
1 package headfirst.designpatterns.iterator.dinermergercafe; 2 3 import java.util.Iterator; 4 5 public class DinerMenuIterator implements Iterator<MenuItem> { 6 MenuItem[] list; 7 int position = 0; 8 9 public DinerMenuIterator(MenuItem[] list) { 10 this.list = list; 11 } 12 13 public MenuItem next() { 14 MenuItem menuItem = list[position]; 15 position = position + 1; 16 return menuItem; 17 } 18 19 public boolean hasNext() { 20 if (position >= list.length || list[position] == null) { 21 return false; 22 } else { 23 return true; 24 } 25 } 26 27 public void remove() { 28 if (position <= 0) { 29 throw new IllegalStateException 30 ("You can't remove an item until you've done at least one next()"); 31 } 32 if (list[position-1] != null) { 33 for (int i = position-1; i < (list.length-1); i++) { 34 list[i] = list[i+1]; 35 } 36 list[list.length-1] = null; 37 } 38 } 39 40 }
2.
1 package headfirst.designpatterns.iterator.dinermergercafe; 2 3 import java.util.Iterator; 4 5 public class DinerMenu implements Menu { 6 static final int MAX_ITEMS = 6; 7 int numberOfItems = 0; 8 MenuItem[] menuItems; 9 10 public DinerMenu() { 11 menuItems = new MenuItem[MAX_ITEMS]; 12 13 addItem("Vegetarian BLT", 14 "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99); 15 addItem("BLT", 16 "Bacon with lettuce & tomato on whole wheat", false, 2.99); 17 addItem("Soup of the day", 18 "Soup of the day, with a side of potato salad", false, 3.29); 19 addItem("Hotdog", 20 "A hot dog, with saurkraut, relish, onions, topped with cheese", 21 false, 3.05); 22 addItem("Steamed Veggies and Brown Rice", 23 "A medly of steamed vegetables over brown rice", true, 3.99); 24 addItem("Pasta", 25 "Spaghetti with Marinara Sauce, and a slice of sourdough bread", 26 true, 3.89); 27 } 28 29 public void addItem(String name, String description, 30 boolean vegetarian, double price) 31 { 32 MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 33 if (numberOfItems >= MAX_ITEMS) { 34 System.err.println("Sorry, menu is full! Can't add item to menu"); 35 } else { 36 menuItems[numberOfItems] = menuItem; 37 numberOfItems = numberOfItems + 1; 38 } 39 } 40 41 public MenuItem[] getMenuItems() { 42 return menuItems; 43 } 44 45 public Iterator<MenuItem> createIterator() { 46 return new DinerMenuIterator(menuItems); 47 //return new AlternatingDinerMenuIterator(menuItems); 48 } 49 50 // other menu methods here 51 }
3.用hashmap
1 package headfirst.designpatterns.iterator.dinermergercafe; 2 3 import java.util.*; 4 5 public class CafeMenu implements Menu { 6 HashMap<String, MenuItem> menuItems = new HashMap<String, MenuItem>(); 7 8 public CafeMenu() { 9 addItem("Veggie Burger and Air Fries", 10 "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", 11 true, 3.99); 12 addItem("Soup of the day", 13 "A cup of the soup of the day, with a side salad", 14 false, 3.69); 15 addItem("Burrito", 16 "A large burrito, with whole pinto beans, salsa, guacamole", 17 true, 4.29); 18 } 19 20 public void addItem(String name, String description, 21 boolean vegetarian, double price) 22 { 23 MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 24 menuItems.put(menuItem.getName(), menuItem); 25 } 26 27 public Map<String, MenuItem> getItems() { 28 return menuItems; 29 } 30 31 public Iterator<MenuItem> createIterator() { 32 return menuItems.values().iterator(); 33 } 34 }
转载于:https://www.cnblogs.com/shamgod/p/5261420.html
HeadFirst设计模式之迭代器模式相关推荐
- 【GOF23设计模式】迭代器模式
[GOF23设计模式]迭代器模式 来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_迭代器模式.JDK内置迭代器.内部类迭代器 1 package com.test.it ...
- [转载] Python进阶:设计模式之迭代器模式
参考链接: Python中的迭代器 在软件开发领域中,人们经常会用到这一个概念--"设计模式"(design pattern),它是一种针对软件设计的共性问题而提出的解决方案.在一 ...
- 每日学一个设计模式1——迭代器模式
引言 精通设计模式是从码农脱颖而出的条件之一.跟着<图解设计模式>这本书学习设计模式,从今天开始,一天总结一个设计模式. 迭代器模式(一个一个遍历) 用处 隐藏遍历集合的内部结构,遍历不同 ...
- php迭代器实例,php设计模式之迭代器模式实例分析【星际争霸游戏案例】
本文实例讲述了php设计模式之迭代器模式.分享给大家供大家参考,具体如下: 星际的任务关一般会有这样的设定:一开始电脑的农民不采矿,如果战斗打响,或者玩家造出第一个兵,电脑的农民开始采矿. 我们自然会 ...
- 设计模式之迭代器模式(Iterator)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于如何创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 设计模式复习-迭代器模式
Iterator.H #pragma once #include <list> #include <windows.h> using namespace std;/* 设计模式 ...
- php foreach 循环 判断index 小于多少_PHP设计模式之迭代器模式 - 硬核项目经理
一说到这个模式,就不得不提循环语句.在<大话设计模式>中,作者说道这个模式现在的学习意义更大于实际意义,这是为什么呢?当然就是被foreach这货给整得.任何语言都有这种类似的语法可以方便 ...
- Java 设计模式之迭代器模式
一.了解迭代器模式 1.1 什么是迭代器模式 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 迭代器模式把游走的任务放在迭代器上,而不是聚合上.这样简化了聚合的接口和 ...
- 设计模式:迭代器模式(Iterator)
欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...
最新文章
- JAVA对象通过jackson转成json格式,属性名首字母变成小写的解决方法
- 阿里云服务器常见用语
- java线程:互斥锁与读写锁
- java如何获得键值_如何在java中取map中的键值 的两种方法
- 【CV秋季划】人脸算法那么多,如何循序渐进地学习好?
- android 恢复短信 失败,解决安卓手机发送短信失败的方法
- 基于单TCP连接的高吞吐模型设计
- 与时间相关NSCalendar、NSDate类的基本使用
- mysql sql语句里连接符的使用_SQL中group_concat函数,用符号连接查询分组里字段值...
- SpringBoot:ApplicationEvent与ApplicationListener
- WP8.1学习系列(第二章)——Toast通知
- 公司绝不会告诉你的20大秘密
- 半监督主动式机器学习的技术优势:银行金融风控场景分析
- 关于js中的冒泡事件
- Cisco路由器配置命令
- 想学习高速ADC/DAC/SDR项目这个项目你不得不理解
- 卷积神经网络常见模块
- 关于未名湖边的烦恼问题
- zabbix-agent配置详解
- 决策树 (Decision Tree) 原理简述及相关算法(ID3,C4.5)