用于容器和容器遍历;容器可以是数组,列表,集合 或者其他的容器型数据结构
迭代器模式提供一种顺序遍历聚合对象元素而不暴露其内部实现方式的方法;
2.角色- Container/Aggregate : 容器类接口
- ConcreteContainer/ConcreteAggregate :具体容器类的实现类,暴露方法创建迭代器
- Iterator:迭代器接口,具有通用方法,hasNext();next()
- ConcreteIterator:一般容器内部会实现自己的迭代器,基于Iterator 接口
Java 版本中的java.util.Collection 和 java.util.Iterator 接口
4.模拟写个例子 4.1.容器和迭代器接口//Container/Aggregate : 容器类接口 interface ICollection4.2.具体容器,具体迭代器(1)extends IIterable { void add(E e); E get(int index); } //Iterator:迭代器接口 interface IIterator { boolean hasNext(); E next(); } //这个接口 用于每个集合的实现类都用方法返回一个迭代器 interface IIterable { IIterator iterator(); }
// ConcreteContainer/ConcreteAggregate :具体容器类的实现类,暴露方法创建迭代器 class IArrayList4.3.具体容器,具体迭代器(2)implements ICollection { private Object[] elements; private int eIndex = 0; public IArrayList() { this.elements = new Object[5]; } @Override public void add(E e) { if (eIndex == (elements.length)) { Object[] elementsNew = new Object[elements.length + (elements.length >> 1)]; System.arraycopy(elements, 0, elementsNew, 0, elements.length); elements = elementsNew; } elements[eIndex] = e; eIndex++; } @Override public E get(int index) { return (E) elements[index]; } //暴露方法创建迭代器 @Override public IIterator iterator() { return new IArrayListIterator(); } //ConcreteIterator:一般容器内部会实现自己的迭代器,基于Iterator 接口 class IArrayListIterator implements IIterator { private int eachIndex = 0; @Override public boolean hasNext() { return eachIndex < eIndex; } @Override public E next() { E element = (E) elements[eachIndex]; eachIndex++; return element; } } }
ConcreteContainer/ConcreteAggregate :具体容器类的实现类,暴露方法创建迭代器 class ILinkedList4.4.客户端测试代码implements ICollection { private Node head; private Node tail; int size = 0; @Override public void add(E e) { Node node = new Node<>(); node.e = e; if (size == 0) { head = node; } else { node.pre = tail; tail.next = node; } tail = node; size++; } @Override public E get(int index) { if (index == 0) { return head.e; } Node node = head; for (int i = 0; i < index; i++) { node = node.next; } return node.e; } //暴露方法创建迭代器 @Override public IIterator iterator() { return new ILinkedListIterator(); } //ConcreteIterator:一般容器内部会实现自己的迭代器,基于Iterator 接口 class ILinkedListIterator implements IIterator { private int eachIndex = 0; @Override public boolean hasNext() { return eachIndex < size; } @Override public E next() { if (eachIndex == 0) { eachIndex++; return head.e; } Node node = head; for (int i = 0; i < eachIndex; i++) { node = node.next; } eachIndex++; return node.e; } } class Node { public Node next; public Node pre; public E e; } }
public static void main(String[] args) {
ICollection collection = new IArrayList<>();
ICollection collection2 = new ILinkedList<>();
//填充数据
for (int i = 0; i < 10; i++) {
collection.add(i + 100);
collection2.add(i * 2);
}
//通用遍历
for (int i = 0; i < 10; i++) {
Integer integer = collection.get(i);
System.out.println("collection index value :" + integer);
}
for (int i = 0; i < 10; i++) {
Integer integer = collection2.get(i);
System.out.println("collection2 index value :" + integer);
}
//迭代器遍历
IIterator iterator = collection.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
System.out.println("iterator collection index value :" + next);
}
IIterator iterator2 = collection2.iterator();
while (iterator2.hasNext()) {
Integer next = iterator2.next();
System.out.println("iterator2 collection index value :" + next);
}
}
测试结果
collection index value :100 collection index value :101 collection index value :102 collection index value :103 collection index value :104 collection index value :105 collection index value :106 collection index value :107 collection index value :108 collection index value :109 collection2 index value :0 collection2 index value :2 collection2 index value :4 collection2 index value :6 collection2 index value :8 collection2 index value :10 collection2 index value :12 collection2 index value :14 collection2 index value :16 collection2 index value :18 iterator collection index value :100 iterator collection index value :101 iterator collection index value :102 iterator collection index value :103 iterator collection index value :104 iterator collection index value :105 iterator collection index value :106 iterator collection index value :107 iterator collection index value :108 iterator collection index value :109 iterator2 collection index value :0 iterator2 collection index value :2 iterator2 collection index value :4 iterator2 collection index value :6 iterator2 collection index value :8 iterator2 collection index value :10 iterator2 collection index value :12 iterator2 collection index value :14 iterator2 collection index value :16 iterator2 collection index value :18 Disconnected from the target VM, address: '127.0.0.1:51888', transport: 'socket' Process finished with exit code 0



