定義:提供一個接口順序訪問一個聚合對象中的各個元素,而又不暴露其內部的實現java
迭代器模式能夠隱藏聚合對象的實現,減小聚合對象的工做算法
使用迭代器訪問元素不保證順序,除非聚合對象有特別說明保證其順序,因此千萬別假定迭代器遍歷出來的元素是有順序的。api
迭代器有內部迭代器和外部迭代器之分,內部迭代器負責迭代,客戶不能控制它的迭代算法,只能將操做傳遞給它,而外部迭代器由客戶控制迭代,好比經過next()方法獲取下一個元素,因此外部迭代器更具備彈性。數組
實現:ide
1.看看Java的聚合對象,如ArrayListui
public Iterator<E> iterator() { return new Itr(); }
private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } @Override @SuppressWarnings("unchecked") public void forEachRemaining(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); final int size = ArrayList.this.size; int i = cursor; if (i >= size) { return; } final Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) { throw new ConcurrentModificationException(); } while (i != size && modCount == expectedModCount) { consumer.accept((E) elementData[i++]); } // update once at end of iteration to reduce heap write traffic cursor = i; lastRet = i - 1; checkForComodification(); } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }
這就是外部迭代器,客戶得到Iterator,本身控制迭代步數和操做this
2.本身實現一個迭代器code
一個汽車4S店汽車清單(數組實現的),使用迭代器方式爲客戶提供遍歷的方式對象
package com.jv.designpattern.iterator; public class Car { private String name; private Long price; public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getPrice() { return price; } public void setPrice(Long price) { this.price = price; } }
package com.jv.designpattern.iterator; import java.util.Iterator; public class CarMenu implements Iterable{ private final static int MAX_LENGTH = 10; private int position = 0; Car[] cars ; public CarMenu(){ cars = new Car[MAX_LENGTH]; } public void addCar(Car car) { cars[position++] = car; } @Override public Iterator iterator() { return new CarMenuIterator(cars); } }
package com.jv.designpattern.iterator; import java.util.Iterator; public class CarMenuIterator implements Iterator{ Car[] cars; private int position = 0; public CarMenuIterator(Car[] cars) { this.cars = cars; } @Override public boolean hasNext() { if(0<=position && position<cars.length-1) { return true; }else { return false; } } @Override public Object next() { return cars[position++]; } }
CarMenu實現Iterable接口獲得更好的彈性,方便被客戶使用接口
CarMenuIterator實現Iterator接口,重寫HashNext和next方法,和其餘Java api保持同樣的迭代方式,減小客戶的實現複雜度