设计模式 - 迭代器模式(iterator pattern) 具体解释
迭代器模式(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 stub
this.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 stub menuItems = 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 stub
menuItems = 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 stub
this.items = items;
} /* (non-Javadoc)
* @see iterator.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
// TODO Auto-generated method stub if (position >= items.length || items[position] == null) {
return false;
} return true;
} /* (non-Javadoc)
* @see iterator.Iterator#next()
*/
@Override
public Object next() {
// TODO Auto-generated method stub MenuItem 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 stub
this.menuItems = menuItems;
} /* (non-Javadoc)
* @see iterator.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if (position >= menuItems.size() || menuItems.get(position) == null) {
return false;
} return true;
} /* (non-Javadoc)
* @see iterator.Iterator#next()
*/
@Override
public Object next() {
// TODO Auto-generated method stub MenuItem 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 stub
this.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 stub
PancakeHouseMenu 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 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
设计模式 - 迭代器模式(iterator pattern) 具体解释的更多相关文章
- C#设计模式——迭代器模式(Iterator Pattern)
一.概述在软件开发过程中,我们可能会希望在不暴露一个集合对象内部结构的同时,可以让外部代码透明地访问其中包含的元素.迭代器模式可以解决这一问题.二.迭代器模式迭代器模式提供一种方法顺序访问一个集合对象 ...
- 设计模式 - 迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释
迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考迭代器模式(ite ...
- 设计模式(十):从电影院中认识"迭代器模式"(Iterator Pattern)
上篇博客我们从醋溜土豆丝与清炒苦瓜中认识了“模板方法模式”,那么在今天这篇博客中我们要从电影院中来认识"迭代器模式"(Iterator Pattern).“迭代器模式”顾名思义就是 ...
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...
- 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...
- 设计模式系列之迭代器模式(Iterator Pattern)——遍历聚合对象中的元素
模式概述 模式定义 模式结构图 模式伪代码 模式改进 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修 ...
- 二十四种设计模式:迭代器模式(Iterator Pattern)
迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...
- 设计模式 - 策略模式(Strategy Pattern) 具体解释
策略模式(Strategy Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879 本文版权全 ...
- 设计模式 - 命令模式(command pattern) 具体解释
命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...
随机推荐
- SSM整合所需jar包
MySql驱动包 mysql-connector-java-5.1.7-bin.jar MyBatis的核心包和依赖包 mybatis-3.2.7.jar(核心包)asm-3.3.1.jar(依赖包) ...
- 区间DP(区间最优解)题目讲解总结
1:给出一个括号字符串,问这个字符串中符合规则的最长子串的长度. [分析]区间DP要覆盖整个区间,那么要求所有情况的并集. 先想出状态方程: dp[i][j]:i ~ j区间内最大匹配数目 输出:dp ...
- Eclipse generate javadoc
注:若遇到导出文档乱码,则点击上图的[next]按钮,在vm options的输入框输入 -J-Xmx180m —- 设置内存大小 (若遇到内存溢出时) -encoding utf-8 ...
- ExtJs之列表常用CRUD
前端代码: Ext.onReady(function(){ Ext.define('Person', { extend: 'Ext.data.Model', fields: [{name: 'id', ...
- 概率dp学习记录
论文参考 汤可因<浅谈一类数学期望问题的解决方法> 反正是很神奇的东西吧..我脑子不好不是很能想得到. bzoj 1415 1415: [Noi2005]聪聪和可可 Time Limit: ...
- ObjC的initialize和init
Objective-C很有趣的一个地方是,它非常非常像C.实际上,它就是C语言加上一些其他扩展和一个运行时间(runtime). 有了这个在每个Objective-C程序中都会起作用的附加运行时间,给 ...
- Hiho : 二分·二分查找之k小数
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回里我们知道Nettle在玩<艦これ>,Nettle的镇守府有很多船位,但船位再多也是有限的.Nettl ...
- NHibernate 存储过程 第十四篇
NHibernate也是能够操作存储过程的,不过第一次配置可能会碰到很多错误. 一.删除 首先,我们新建一个存储过程如下: CREATE PROC DeletePerson @Id int AS DE ...
- Word中正文两栏表格通栏排版
如图所示,需要在插入表格的地方插入连续分节符,这是word2013,在插入里面只有分页符 如果直接插入的话表格上下的分节符会分栏,那样表格会歪曲, 可以上面一段和下面一段设置成通栏,然后中间插入表格, ...
- winform 使用SplashScreen窗口
SplashScreen,就是平时我们说的溅射屏幕,任何一个做过客户端程序的coder应该对它都不陌生,因为它能提升用户体验,让软件看上去更美.SplashScreenForm通常进入程序时是打开,主 ...