迭代器模式(iterator pattern) 详细解释

本文地址: http://blog.csdn.net/caroline_wendy

迭代器模式(iterator pattern) : 提供一种方法顺序訪问一个聚合对象中的各个元素, 而又不暴露其内部的表示;

建立迭代器接口(iterator interface), 包括hasNext()方法和next()方法;

不同聚合对象的详细的迭代器(concrete iterator), 继承(implements)迭代器接口(iterator interface), 实现hasNext()方法和next()方法;

详细聚合对象(concrete aggregate), 提供创建迭代器的方法(createIterator).

通过调用聚合对象的迭代器, 就可以使用迭代器, 统一输出接口.

面向对象设计原则:

一个类应该仅仅有一个引起变化的原因.

即一个类应该仅仅有一个责任, 即高内聚.

详细方法:

1. 菜单项, ArrayList和数组的基本元素.

/*** @time 2014年6月20日*/
package iterator;/*** @author C.L.Wang**/
public class MenuItem {String name;String description;boolean vegetarian; //是否是素食double price;/*** */public MenuItem(String name, String description, boolean vegetarian, double price) {// TODO Auto-generated constructor stubthis.name = name;this.description = description;this.vegetarian = vegetarian;this.price = price;}public String getName() {return name;}public String getDescription() {return description;}public double getPrice() {return price;}public boolean isVegetarian() {return vegetarian;}}

2. 详细的聚合对象(concrete aggregate), 包括创建迭代器的方法(createIterator).

/*** @time 2014年6月26日*/
package iterator;/*** @author C.L.Wang**/
public class DinerMenu {static final int MAX_ITEMS = 6;int numberOfItems = 0;MenuItem[] menuItems;/*** */public DinerMenu() {// TODO Auto-generated constructor stubmenuItems = 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 the 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 vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);if (numberOfItems >= MAX_ITEMS) {System.err.println("Sorry, menu is full! Can't add item to menu");} else {menuItems[numberOfItems] = menuItem;++numberOfItems;}}public Iterator createIterator() {return new DinerMenuIterator(menuItems);}}/*** @time 2014年6月20日*/
package iterator;import java.util.ArrayList;/*** @author C.L.Wang**/
public class PancakeHouseMenu {ArrayList<MenuItem> menuItems;/*** */public PancakeHouseMenu() {// TODO Auto-generated constructor stubmenuItems = new ArrayList<MenuItem>();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 made 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 Iterator createIterator() {return new PancakeHouseMenuIterator(menuItems);}}

3. 迭代器接口(iterator interface), 包括hasNext()方法next()方法.

/*** @time 2014年6月27日*/
package iterator;/*** @author C.L.Wang**/
public interface Iterator {boolean hasNext();Object next();}

4. 聚合对象的详细的迭代器(concrete iterator)继承(implements)迭代器接口(iterator interface), 实现hasNext()方法和next()方法;

/*** @time 2014年6月27日*/
package iterator;/*** @author C.L.Wang**/
public class DinerMenuIterator implements Iterator {MenuItem[] items;int position = 0;/*** */public DinerMenuIterator(MenuItem[] items) {// TODO Auto-generated constructor stubthis.items = items;}/* (non-Javadoc)* @see iterator.Iterator#hasNext()*/@Overridepublic boolean hasNext() {// TODO Auto-generated method stubif (position >= items.length || items[position] == null) {return false;}return true;}/* (non-Javadoc)* @see iterator.Iterator#next()*/@Overridepublic Object next() {// TODO Auto-generated method stubMenuItem menuItem = items[position];++position;return menuItem;}}/*** @time 2014年6月27日*/
package iterator;import java.util.ArrayList;/*** @author C.L.Wang**/
public class PancakeHouseMenuIterator implements Iterator {ArrayList<MenuItem> menuItems;int position;/*** */public PancakeHouseMenuIterator(ArrayList<MenuItem> menuItems) {// TODO Auto-generated constructor stubthis.menuItems = menuItems;}/* (non-Javadoc)* @see iterator.Iterator#hasNext()*/@Overridepublic boolean hasNext() {// TODO Auto-generated method stubif (position >= menuItems.size() || menuItems.get(position) == null) {return false;}return true;}/* (non-Javadoc)* @see iterator.Iterator#next()*/@Overridepublic Object next() {// TODO Auto-generated method stubMenuItem menuItem = menuItems.get(position);++position;return menuItem;}}

5. 客户类使用详细聚合类(concrete aggregate)迭代器(iterator).

/*** @time 2014年6月27日*/
package iterator;/*** @author C.L.Wang**/
public class Waitress {PancakeHouseMenu pancakeHouseMenu;DinerMenu dinerMenu;/*** */public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {// TODO Auto-generated constructor stubthis.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;}public void printMenu() {Iterator pancakeIterator = pancakeHouseMenu.createIterator();Iterator dinerIterator = dinerMenu.createIterator();System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);}private void printMenu(Iterator iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();System.out.print(menuItem.getName() + ": ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}}}

6. 測试代码:

/*** @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();Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);waitress.printMenu();}}

7. 输出:

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 strawberriesLUNCH
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

设计模式 - 迭代器模式(iterator pattern) 具体解释相关推荐

  1. 设计模式:迭代器模式(Iterator Pattern)

    迭代器模式(Iterator Pattern): 属于行为型模式,提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需要知道集合对象的底层表示,即: 不暴露其内部结构.

  2. 解读设计模式----迭代器模式(Iterator Pattern),谁才是迭代高手

    一.你在开发中使用过迭代吗?      当你在使用JavaScript开发客户端应用的时候使用过for...in吗?  1<script type="text/javascript&q ...

  3. 听webcast的行为型模式篇-迭代器模式(Iterator Pattern) 记录

    < DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd> dotnet或java里 ...

  4. 迭代器模式(Iterator pattern)

    一. 引言 迭代这个名词对于熟悉Java的人来说绝对不陌生.我们常常使用JDK提供的迭代接口进行java collection的遍历: Iterator it = list.iterator(); w ...

  5. 【java设计模式】【行为模式Behavioral Pattern】迭代器模式Iterator Pattern

    1 package com.tn.pattern; 2 3 public class Client { 4 public static void main(String[] args) { 5 Obj ...

  6. 33迭代器模式(Iterator Pattern)

    动机(Motivate):     在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码透明地访问其中包含的元素;同时这种" ...

  7. java设计模式迭代器模式_Java中的迭代器设计模式

    java设计模式迭代器模式 Iterator design pattern in one of the behavioral pattern. Iterator pattern is used to ...

  8. 设计模式——迭代器模式(遍历王者荣耀和英雄联盟英雄信息)

    本文首发于cdream的个人博客,点击获得更好的阅读体验! 欢迎转载,转载请注明出处. 本文主要讲述迭代器模式,并使用遍历不同数据结构的王者荣耀和英雄联盟英雄作为例子帮助大家理解,最后附上阿离美图一张 ...

  9. 设计模式——迭代器模式(附代码示例)

    一. 传统方式 以学校院系展示为例,实现在一个页面展示学校的院系组成,一个学校有多个学院,一个学院有多个系.传统方式实现类图如下: 传统方式将学院看做是学校的子类,系是学院的子类,这样实际上是站在组织 ...

最新文章

  1. SVN地址正确,能在网页打开,但是检出失败解决方法
  2. 域名解析服务查询工具dnstracer
  3. 月薪没过20K的程序员要注意了!(文末送书)
  4. 22. Leetcode 237. 删除链表中的节点 (链表-基础操作类-删除链表的节点)
  5. Android方法的概括,android中的Filter接口简介
  6. OPPO Reno造乐节落地重庆 华语乐坛十大金曲榜单公布
  7. python输入时间限制_用Python计算用户输入时间
  8. 【GStreamer开发】GStreamer基础教程07——多线程和Pad的有效性
  9. 我是如何在5 天内,完成 60 个类的核心模块的重构
  10. 王道计算机考研图书勘误表公布!
  11. oracle11gora1435,oracle导入的问题
  12. 皇家每羊历险记(二)——地形制作
  13. 颜色的前世今生13·RGB显色系统详解(下)
  14. Qt获取QTextEdit中的内容
  15. 【机器学习】最大均值差异MMD详解
  16. 微型计算机的发展经历了哪几个,计算机的小故事有哪些_计算机发展史小故事...
  17. shell脚本编程学习笔记5(xdl)——正则表达式
  18. 安卓和ios的app测试有什么区别?
  19. C语言函数(函数嵌套、递归调用)+局部变量和全局变量+extern关键字的使用+Visual Studio简单的使用教程+数据存储类别+内部函数外部函数
  20. 教链一周谈20200222

热门文章

  1. Cisco3560交换机enable密码破解和恢复出厂设置
  2. SQL Server中的锁类型及用法(转载)
  3. 近期PHP很火爆,弄了个Discuz耍耍
  4. 半木下低风险交易-1
  5. Xamarin Forms启动自带模拟器缓慢
  6. itx机箱尺寸_乔思伯发布ITX机箱V8,采用独特抽拉式结构
  7. c语言的指针和java_C语言指针变量的定义和使用(精华)
  8. Oracle的ONS创建,Oracle 10gR2 RAC Clusterware ONS服务的管理
  9. 使用脑电图慢皮层电位重建3D空间中的手,肘和肩的实际和想象的轨迹
  10. 将深度学习技术应用于基于情境感知的情绪识别