博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式 - 迭代器模式(iterator pattern) 具体解释
阅读量:5966 次
发布时间:2019-06-19

本文共 6888 字,大约阅读时间需要 22 分钟。

迭代器模式(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 menuItems;		/**	 * 	 */	public PancakeHouseMenu() {		// TODO Auto-generated constructor stub		menuItems = new ArrayList();				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 menuItems;	int position;		/**	 * 	 */	public PancakeHouseMenuIterator(ArrayList 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----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 cheese

你可能感兴趣的文章
telnet :No route to host
查看>>
基本安装lnmp环境
查看>>
yum源资料汇总
查看>>
7、MTC与MTV,http请求介绍
查看>>
logstash消费阿里云kafka消息
查看>>
第四节课作业
查看>>
EasyUI Calendar 日历
查看>>
unix 环境高级编程
查看>>
为数据库建立索引
查看>>
第二周作业-软件工作量的估计
查看>>
MAXIMO 快速查找实现
查看>>
Oracle——条件控制语句
查看>>
[Linux][Redis][05]Benchmark
查看>>
第一次作业-准备篇
查看>>
HDU1848 Fibonacci again and again
查看>>
HTML思维导图
查看>>
git改密码出现授权问题
查看>>
ORA-02266: 表中的唯一/主键被启用的外键引用
查看>>
Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法
查看>>
day-6 and day-7:面向对象
查看>>