正文
一、定義
迭代器模式提供一種方法順序訪問一個聚合對象中的各個元素,而又不暴露其內部的表示。
要點:
- 迭代器模式把在元素之間遊走的責任交給迭代器,而不是聚合對象。這樣簡化了聚合的接口和實現,也讓責任各得其所。
二、實現步驟
1、創建迭代器接口
/**
* 迭代器接口
*/
public interface Iterator {
/**
* 是否有下一個元素
*/
public boolean hasNext();
/**
* 獲取下一個元素
*/
public Object next();
}
2、創建具體迭代器,並實現迭代器接口
具體迭代器負責遍歷元素,以及管理目前遍歷的位置。
/**
* 具體迭代器
*/
public class ConcreteIterator implements Iterator {
/**
* 要遍歷的集合(數組)
*/
public String[] items;
/**
* 當前遍歷位置
*/
int position = 0;
public ConcreteIterator(String[] items) {
this.items = items;
}
@Override
public boolean hasNext() {
if (position < items.length) {
return true;
}
return false;
}
@Override
public Object next() {
if (!this.hasNext()) {
return null;
}
String item = items[position];
position = position + 1;
return item;
}
}
3、創建聚合接口,並定義返回迭代器的方法
/**
* 聚合接口
*/
public interface Aggregate {
/**
* 創建迭代器
*/
public Iterator createIterator();
}
4、創建具體聚合,並實現返回迭代器的方法
具體聚合裏面持有集合。這裏的集合指的是一羣對象,其存儲方式可以是列表、數組、散列表等。
/**
* 具體聚合
*/
public class ConcreteAggregate implements Aggregate {
/**
* 持有集合(比如列表、數組、散列表等)
*/
public String[] items = new String[] {"item1", "item2", "item3"};
@Override
public Iterator createIterator() {
return new ConcreteIterator(items);
}
}
5、使用迭代器進行遍歷
public class Test {
public static void main(String[] args) {
// 聚合對象
Aggregate aggregate = new ConcreteAggregate();
// 迭代器
Iterator iterator = aggregate.createIterator();
// 遍歷
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
三、舉個栗子
1、背景
對象村餐廳和對象村煎餅屋合併了,現在我們可以在同一個地方,享用煎餅屋美味的煎餅早餐和好喫的餐廳午餐了。
假設你被他們合組的新公司僱傭,打算創建一個 Java 版本的女招待。這個 Java 版本的女招待規格是:能應對顧客的需要打印定製的菜單,而無需詢問廚師。
現在,有一點小麻煩:煎餅屋菜單使用 ArrayList 記錄菜單項,而餐廳則是使用數組記錄菜單項。兩者都不願意改變他們的實現,畢竟有太多代碼依賴於它們了。
好消息是,煎餅屋和餐廳都同意實現一個統一的菜單項 MenuItem。
2、實現
將集合的遍歷交給迭代器,這樣就不用關心菜單是使用 ArrayList 還是數組記錄菜單項了。
(1)創建菜單項
/**
* 菜單項
*/
public class MenuItem {
String name;
double price;
public MenuItem(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;
}
}
(2)創建迭代器接口
/**
* 迭代器接口
*/
public interface Iterator {
/**
* 是否有下一個元素
*/
public boolean hasNext();
/**
* 獲取下一個元素
*/
public Object next();
}
(3)創建具體的菜單迭代器
/**
* 煎餅屋菜單迭代器
*/
public class PancakeHouseMenuIterator implements Iterator {
/**
* 列表形式的菜單項
*/
public ArrayList<MenuItem> menuItems;
int position = 0;
public PancakeHouseMenuIterator(ArrayList<MenuItem> items) {
this.menuItems = items;
}
@Override
public boolean hasNext() {
if (position < menuItems.size()) {
return true;
}
return false;
}
@Override
public Object next() {
if (!this.hasNext()) {
return null;
}
MenuItem item = menuItems.get(position);
position = position + 1;
return item;
}
}
/**
* 餐廳菜單迭代器
*/
public class DinerMenuIterator implements Iterator {
/**
* 數組形式的菜單項
*/
public MenuItem[] menuItems;
int position = 0;
public DinerMenuIterator(MenuItem[] items) {
this.menuItems = items;
}
@Override
public boolean hasNext() {
if (position < menuItems.length) {
return true;
}
return false;
}
@Override
public Object next() {
if (!this.hasNext()) {
return null;
}
MenuItem item = menuItems[position];
position = position + 1;
return item;
}
}
(4)創建菜單接口
/**
* 菜單接口
*/
public interface Menu {
/**
* 創建迭代器
*/
public Iterator createIterator();
}
(5)創建具體的菜單
/**
* 煎餅屋菜單
*/
public class PancakeHouseMenu implements Menu {
public ArrayList<MenuItem> menuItems;
public PancakeHouseMenu() {
menuItems = new ArrayList<MenuItem>();
menuItems.add(new MenuItem("Regular Pancake Breakfast", 2.99));
menuItems.add(new MenuItem("Blueberry Pancakes", 3.49));
menuItems.add(new MenuItem("Waffles", 3.59));
}
@Override
public Iterator createIterator() {
return new PancakeHouseMenuIterator(menuItems);
}
}
/**
* 餐廳菜單
*/
public class DinerMenu implements Menu {
public MenuItem[] menuItems;
public DinerMenu() {
menuItems = new MenuItem[3];
menuItems[0] = new MenuItem("BLT", 2.99);
menuItems[1] = new MenuItem("Soup of the day", 3.29);
menuItems[2] = new MenuItem("Hotdog", 3.05);
}
@Override
public Iterator createIterator() {
return new DinerMenuIterator(menuItems);
}
}
(6)創建女招待
/**
* 女招待
*/
public class Waitress {
Menu pancakeHouseMenu;
Menu dinerMenu;
public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinerMenu = dinerMenu;
}
public void printMenu() {
// 獲取菜單迭代器
Iterator pancakeIterator = pancakeHouseMenu.createIterator();
Iterator dinerIterator = dinerMenu.createIterator();
// 使用迭代器打印菜單
System.out.println("----MENU----\n");
System.out.println("BREAKFASE:");
printMenu(pancakeIterator);
System.out.println("\nLUNCH:");
printMenu(dinerIterator);
}
private void printMenu(Iterator iterator) {
while (iterator.hasNext()) {
MenuItem menuItem = (MenuItem)iterator.next();
System.out.println(menuItem.getName() + ", " + menuItem.getPrice());
}
}
}
(7)使用女招待打印菜單
public class Test {
public static void main(String[] args) {
// 菜單
Menu pancakeHouseMenu = new PancakeHouseMenu();
Menu dinerMenu = new DinerMenu();
// 女招待
Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);
// 打印菜單
waitress.printMenu();
}
}