声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/
import java.util.Arrays;
import java.util.List;
/**
* Iterator模式提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象内部表示
*
* 个人觉得,为了不暴露该对象内部表示,而额外地提供一组访问接口(通用的遍历接口)
* 具体来说,就是想办法在被遍历的对象里面提供一个获得Iterator的方法: Iterator iterator(){...}
* 然后利用这个Iterator去遍历
* Iterator里面实现了通用的遍历方法:first(),next(),isDone(),currItem()
*
* 书上还提到一个很实用的例子,就是迭代器实现翻页
*/
interface Iterator {
void first();
void next();
boolean isDone();
Object currItem();
}
abstract class Aggregate {
public abstract Iterator iterator();
}
//需要用迭代器遍历的对象
class ArrayAggregate extends Aggregate {
private String[] data;
public ArrayAggregate(String[] data) {
this.data = data;
}
@Override
public Iterator iterator() {
return new ArrayIterator(this);
}
public Object get(int index) {
Object item = null;
if (index < data.length) {
item = data[index];
}
return item;
}
public int size() {
return data.length;
}
}
//需要用迭代器遍历的对象
class ListAggregate extends Aggregate {
private List<String> data;
public ListAggregate(List<String> data) {
this.data = data;
}
@Override
public Iterator iterator() {
return new ListIterator(this);
}
public int size() {
return data.size();
}
public Object get(int index) {
Object item = null;
if (index < data.size()) {
item = data.get(index);
}
return item;
}
}
class ArrayIterator implements Iterator {
private int index;
/* 如果是带迭代策略的Iterator,那ArrayIterator持有的就不是整个ArrayAggregate,
* 而是
1.ArrayAggregate过滤后的部分数据(例如只允许访问第一条记录),
可在构造函数里过滤:
private String[] dataAfterFilter;
public ArrayIterator(ArrayAggregate aggregate) {
dataAfterFilter =new String[]{(String) aggregate.get(0)};
}
2.从数据库取指定数量的指定记录(翻页)
3.等等
*/
private ArrayAggregate aggregate;
public ArrayIterator(ArrayAggregate aggregate) {
this.aggregate = aggregate;
}
//first(),next(),isDone(),currItem()这些方法,最终还是调用被遍历的对象的方法来实现
public void first() {
index = 0;
}
public void next() {
if (index < aggregate.size()) {
index++;
}
}
public boolean isDone() {
return index == aggregate.size();
}
public Object currItem() {
return aggregate.get(index);
}
}
class ListIterator implements Iterator {
private ListAggregate aggregate;
private int index;
public ListIterator(ListAggregate aggregate) {
this.aggregate = aggregate;
}
public void first() {
index = 0;
}
public void next() {
if (index < aggregate.size()) {
index++;
}
}
public boolean isDone() {
return index == aggregate.size();
}
public Object currItem() {
return aggregate.get(index);
}
}
public class IteratorPattern {
public static void main(String[] args) {
String[] array = { "aa", "bb", "cc" };
ArrayAggregate aggregate2 = new ArrayAggregate(array);
Iterator iterator2 = aggregate2.iterator();
visit(iterator2);
List<String> list = Arrays.asList(array); // asList返回的是一个不可修改的List,只供遍历
ListAggregate aggregate = new ListAggregate(list);
Iterator iterator = aggregate.iterator();
visit(iterator);
}
public static void visit(Iterator iterator) {
iterator.first();
while (!iterator.isDone()) {
Object obj = iterator.currItem();
System.out.println(obj);
iterator.next();
}
}
}