一、迭代器模式
提供一种方法顺序访问一个聚合对象(也叫集合对象,比如ArrayList)中的各个元素,而不是暴露其内部的表示。
二、设计原则
1、一个类应该只有一个引起变化的原因
2、类的每个责任都有改变的潜在区域。超过一个责任,意味着超过一个改变的区域。
3、这个原则告诉我们尽量让每个类保持单一的责任。
三、项目背景
煎饼屋餐厅要和晚餐屋餐厅合并,煎饼屋的菜单是通过数组实现的,晚餐屋的菜单是通过集合实现了,双方都不想改变自己的菜单结构,但又要呈现完整的菜单,这无疑是对两个菜单进行for循环取出,读取菜品,然后这样并不方便代码管理。
代码如下:
public class DinnerMenu {
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem[] menuItems;
public DinnerMenu() {
// TODO Auto-generated constructor stub
menuItems = new MenuItem[MAX_ITEMS];
addItem("午餐A", "好吃的午餐A", true, 2.99);
addItem("午餐B", "好吃的午餐B", true, 3.99);
addItem("午餐C", "好吃的午餐C", true, 4.99);
addItem("午餐D", "好吃的午餐D", true, 5.99);
addItem("午餐E", "好吃的午餐E", true, 6.99);
addItem("午餐F", "好吃的午餐F", true, 6.99);
}
public void addItem(String name,String description,
boolean vegetarian,double price){
MenuItem item = new MenuItem(name, description, vegetarian, price);
if(numberOfItems >= MAX_ITEMS){
System.out.println("对不起,菜单栏已满,无法再继续添加菜品");
}else{
menuItems[numberOfItems] = item;
numberOfItems = numberOfItems + 1;
}
}
public MenuItem[] getMenuItems() {
return menuItems;
}
}
//菜单项
public class MenuItem {
String name;
String description;
boolean vegetarian;
double price;
public MenuItem(String name,
String description,
boolean vegetarian,
double price) {
this.name = name;
this.description = description;
this.vegetarian = vegetarian;
this.price = price;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public boolean isVegetarian() {
return vegetarian;
}
public double getPrice() {
return price;
}
}
//煎饼屋
public class PancakeHouseMenu {
ArrayList<MenuItem> menuItems;
public PancakeHouseMenu() {
// TODO Auto-generated constructor stub
menuItems = new ArrayList<MenuItem>();
addItem("煎饼A", "好吃的煎饼A", true, 2.99);
addItem("煎饼B", "好吃的煎饼B", true, 3.99);
addItem("煎饼C", "好吃的煎饼C", true, 4.99);
addItem("煎饼D", "好吃的煎饼D", true, 5.99);
addItem("煎饼E", "好吃的煎饼E", true, 6.99);
}
public void addItem(String name,String description,
boolean vegetarian,double price){
MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
menuItems.add(menuItem);
}
public ArrayList<MenuItem> getMenuItems() {
return menuItems;
}
}
public class Test {
public static void main(String[] args) {
/*
* 方法看起来一样,但是调用所返回的结果却是不一样类型
*/
PancakeHouseMenu houseMenu = new PancakeHouseMenu(); //煎饼菜单
ArrayList breakfastItems = houseMenu.getMenuItems();
DinnerMenu dinnerMenu = new DinnerMenu(); //早晨项
MenuItem[] lunchItems = dinnerMenu.getMenuItems();
//都是for循环
for(int i = 0; i < breakfastItems.size(); i++){
MenuItem item = (MenuItem) breakfastItems.get(i);
System.out.println(item.getName() + " ");
System.out.println(item.getPrice() + " ");
System.out.println(item.getDescription());
}
System.out.println("---------------------------------------------");
for (int i = 0; i < lunchItems.length; i++) {
MenuItem item = lunchItems[i];
System.out.println(item.getName() + " ");
System.out.println(item.getPrice() + " ");
System.out.println(item.getDescription());
}
}
}
四、迭代器模式
package com.oyhp.iterator;
//自定义迭代器
public interface Iterator {
boolean hasNext();
Object next();
}
package com.oyhp.iterator;
//这是一个简单接口,让客户能够取得一个菜单项迭代器
public interface Menu {
public Iterator createIterator();
}
package com.oyhp.iterator;
import com.oyhp.menu.MenuItem;
public class DinnerMenu implements Menu{
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
MenuItem[] menuItems;
public DinnerMenu() {
// TODO Auto-generated constructor stub
menuItems = new MenuItem[MAX_ITEMS];
addItem("午餐A", "好吃的午餐A", true, 2.99);
addItem("午餐B", "好吃的午餐B", true, 3.99);
addItem("午餐C", "好吃的午餐C", true, 4.99);
addItem("午餐D", "好吃的午餐D", true, 5.99);
addItem("午餐E", "好吃的午餐E", true, 6.99);
addItem("午餐F", "好吃的午餐F", true, 6.99);
}
public void addItem(String name,String description,
boolean vegetarian,double price){
MenuItem item = new MenuItem(name, description, vegetarian, price);
if(numberOfItems >= MAX_ITEMS){
System.out.println("对不起,菜单栏已满,无法再继续添加菜品");
}else{
menuItems[numberOfItems] = item;
numberOfItems = numberOfItems + 1;
}
}
public Iterator createIterator(){
return new DinnerMenuIterator(menuItems);
}
}
//晚餐迭代器
import com.oyhp.menu.MenuItem;
public class DinnerMenuIterator implements Iterator{
MenuItem[] items;
int position = 0;
public DinnerMenuIterator(MenuItem[] items) {
this.items = items;
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if(position >= items.length || items[position] == null){
return false;
}else{
return true;
}
}
@Override
public Object next() {
// TODO Auto-generated method stub
MenuItem menuItem = items[position];
position = position + 1;
return menuItem;
}
}
public class PancakeHouseIterator implements Iterator{
ArrayList menuItems;
int position = 0;
public PancakeHouseIterator(ArrayList menuItems) {
this.menuItems = menuItems;
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if(position < menuItems.size()){
return true;
}else{
return false;
}
}
@Override
public Object next() {
// TODO Auto-generated method stub
MenuItem item = (MenuItem) menuItems.get(position);
position = position + 1;
return item;
}
}
//煎饼屋
public class PancakeHouseMenu implements Menu{
ArrayList<MenuItem> menuItems;
public PancakeHouseMenu() {
// TODO Auto-generated constructor stub
menuItems = new ArrayList<MenuItem>();
addItem("煎饼A", "好吃的煎饼A", true, 2.99);
addItem("煎饼B", "好吃的煎饼B", true, 3.99);
addItem("煎饼C", "好吃的煎饼C", true, 4.99);
addItem("煎饼D", "好吃的煎饼D", true, 5.99);
addItem("煎饼E", "好吃的煎饼E", true, 6.99);
}
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 PancakeHouseIterator(menuItems);
}
}
//服务员
public class Waitress {
ArrayList menus; //用于把菜单项封装起来,比如煎饼屋和午餐菜单
Menu houseMenu;
Menu dinnerMenu;
//构造函数初始化实例
//Menu 将具体菜单类改成了Menu接口,使其更加的有弹性,符合针对接口编程,少用实现编程
public Waitress(Menu houseMenu,Menu dinnerMenu) {
// TODO Auto-generated constructor stub
this.houseMenu = houseMenu;
this.dinnerMenu = dinnerMenu;
}
/*
* 为了使用"集合"自带系统的迭代器,进行初始化
*/
public Waitress(ArrayList menus){
this.menus = menus;
}
//打印菜单ArrayList,通过封装菜单项,再通过迭代器依次取出菜单项,在打印菜单项。
public void printArrayListMenu(){
java.util.Iterator menusIterator = menus.iterator(); //系统自带迭代器
while(menusIterator.hasNext()){
Menu menu = (Menu) menusIterator.next();
printMenu(menu.createIterator());
}
}
//打印菜单
public void printMenu(){
Iterator pancakeIterator = houseMenu.createIterator();
Iterator dinnerIterator = dinnerMenu.createIterator();
System.out.println("MENU\n------\nBREAKFAST");
printMenu(pancakeIterator);
System.out.println("\nLUNCH");
printMenu(dinnerIterator);
}
private void printMenu(Iterator iterator){
while(iterator.hasNext()){
MenuItem item = (MenuItem) iterator.next();
System.out.println(item.getName() + ", ");
System.out.println(item.getPrice() + " -- ");
System.out.println(item.getDescription());
}
}
}
public class MenuTestDrive {
//测试迭代器
@Test
public void testIterator(){
PancakeHouseMenu houseMenu = new PancakeHouseMenu();
DinnerMenu dinnerMenu = new DinnerMenu();
Waitress waitress = new Waitress(houseMenu, dinnerMenu);
System.out.println("--------------------测试迭代器-------------------------");
waitress.printMenu();
}
//测试封装了菜单项的迭代器
@Test
public void testArraylistIterator(){
System.out.println("------------------测试封装了菜单项的迭代器------------------");
ArrayList menus = new ArrayList<Menu>();
PancakeHouseMenu houseMenu = new PancakeHouseMenu();
DinnerMenu dinnerMenu = new DinnerMenu();
menus.add(houseMenu);
menus.add(dinnerMenu);
Waitress waitress = new Waitress(menus);
waitress.printArrayListMenu();
}
}
五、迭代器类图
六、总结
1、迭代器模式让我们能游走于集合内的每一个元素,而不是暴露其内部的表示。
2、集合把游走的任务放在迭代器上,而不是集合上。这样简化了集合的接口和实现,也让责任各得其所。