迭代器模式是针对集合对象而生的,对于集合对象而言,肯定会涉及到对集合的添加和删除操作,同时也肯定支持遍历集合元素的操作,我们此时可以把遍历操作放在集合对象中,但这样的话,集合对象既承担太多的责任了,面向对象设计原则中有一条就是单一职责原则,所有我们要尽可能地分离这些职责,用不同的类取承担不同的责任,迭代器模式就是用迭代器类来承担遍历集合的职责。
定义:迭代器模式提供了一种方法顺序访问一个聚合对象中的各个元素,而又无需暴露该对象的内部实现,这样既可以做到不暴露集合的内部结构,又可让外部代码透明地访问集合内部的数据
/** * 菜单 */ public class Menu { private String name; private Double price; public Menu(String name, Double price) { this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } }
/** * 晚餐店 */ public class DinnerShop { private static final int MAX_ITEMS = 4; int index = 0; Menu[] menuItems; public DinnerShop() { menuItems = new Menu[MAX_ITEMS]; // 默认就放入菜单项 addItem("浪漫晚餐", 299d); addItem("小龙虾", 99d); addItem("鱿鱼", 49d); addItem("扇贝", 89d); } public void addItem(String name, double price) { Menu menu = new Menu(name, price); if (index >= MAX_ITEMS) { System.err.println("对不起, 菜单页满了"); } else { menuItems[index] = menu; index++; } } // 创建迭代器:重点 public Iterator createIteraotr() { return new DinnerMenuIterator(menuItems); } }
/** * 晚餐菜单遍历类 */ public class DinnerMenuIterator implements Iterator { Menu[] menuItems; int position = 0; public DinnerMenuIterator(Menu[] menuItems) { this.menuItems = menuItems; } @Override public boolean hasNext() { if (position >= menuItems.length || menuItems[position] == null) { return false; } return true; } @Override public Object next() { Menu menuItem = menuItems[position]; position++; return menuItem; } }
/** * 汉堡店菜单 */ public class PancakeHouseShop { /** * 菜单列表 */ List<Menu> menuItems; public PancakeHouseShop() { this.menuItems = new ArrayList<>(); // 默认就放入菜单项 addItem("可乐", 3d); addItem("汉堡", 13d); addItem("薯条", 8d); addItem("鸡翅", 5d); } public void addItem(String name, Double price) { Menu menu = new Menu(name, price); menuItems.add(menu); } // 产生迭代器 public Iterator createIterator() { return new PancakeHouseMenuIterator(menuItems); } }
/** * 汉堡包菜单迭代类 */ public class PancakeHouseMenuIterator implements Iterator { List<Menu> menuItems; int position = 0; public PancakeHouseMenuIterator(List<Menu> menuItems) { this.menuItems = menuItems; } @Override public boolean hasNext() { if (position >= menuItems.size() || menuItems.get(position) == null) { return false; } return true; } @Override public Object next() { Menu menuItem = menuItems.get(position); position++; return menuItem; } }
/** * 服务生 */ public class Waiter { public void print() { System.out.println("================== 汉堡包菜单 ======================"); PancakeHouseShop pancakeHouseShop = new PancakeHouseShop(); Iterator pancakeHouseMenuIterator = pancakeHouseShop.createIterator(); printMenu(pancakeHouseMenuIterator); System.out.println("=================== 晚餐菜单 ====================="); DinnerShop dinnerShop = new DinnerShop(); Iterator dinnerMenuIterator = dinnerShop.createIteraotr(); printMenu(dinnerMenuIterator); } private void printMenu(Iterator iterator) { while (iterator.hasNext()) { Menu menu = (Menu) iterator.next(); System.out.println("名称: " + menu.getName() + "======== 价格: " + menu.getPrice()); } } public static void main(String[] args) { Waiter waiter = new Waiter(); waiter.print(); } }控制台打印: ================== 汉堡包菜单 ======================名称: 可乐======== 价格: 3.0名称: 汉堡======== 价格: 13.0名称: 薯条======== 价格: 8.0名称: 鸡翅======== 价格: 5.0=================== 晚餐菜单 =====================名称: 浪漫晚餐======== 价格: 299.0名称: 小龙虾======== 价格: 99.0名称: 鱿鱼======== 价格: 49.0名称: 扇贝======== 价格: 89.0