模式的定义
迭代器模式定义:
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
提供一种方法访问一个容器对象中各个元素,而又不需要暴露对象的内部细节。
类型
行为类
模式的使用场景
方便遍历访问容器内的元素
优点
面向对象设计原则中的单一职责原则,对于不同的功能,我们要尽可能的把这个功能分解出单一的职责,不同的类去承担不同的职责。Iterator模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样不暴露集合的内部结构,又可让外部代码透明的访问集合内部的数据
缺点
- 会产生多余的对象,消耗内存;
- 遍历过程是一个单向且不可逆的遍历
- 如果你在遍历的过程中,集合发生改变,变多变少,内容变化都是算,就会抛出来ConcurrentModificationException异常.
UML类图
角色介绍
Iterator –抽象迭代器
抽象迭代器负责定义访问和遍历元素的接口,而且有固定的3个方法:first()获得第一个元素,next()访问下一个元素,hasNext()是否有下一个元素。
ConcreteIterator —具体迭代器
实现迭代器接口,完成容器元素遍历
Aggregate —抽象容器
提供一个iterator()方法
ConcreteAggregate —具体容器
实现抽象容器的接口
模式的通用源码
Iterator :
public interface Iterator {
public Object next();
public boolean hasNext();
public boolean remove();
}
ConcreteIterator :
import java.util.Vector;
public class ConcreteIterator implements Iterator {
private Vector vector = new Vector();
private int cursor = 0;
public ConcreteIterator(Vector vector) {
super();
this.vector = vector;
}
@Override
public Object next() {
// TODO Auto-generated method stub
Object result;
if(hasNext()){
result = vector.get(cursor++);
}else {
result = null;
}
return result;
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if(cursor == vector.size()){
return false;
}else{
return true;
}
}
@Override
public boolean remove() {
// TODO Auto-generated method stub
vector.remove(cursor);
return true;
}
}
Aggregate :
public interface Aggregate {
public void add(Object object);
public void remove(Object object);
public Iterator iterator();
}
ConcreteAggregate :
import java.util.Vector;
public class ConcreteAggregate implements Aggregate {
private Vector vector = new Vector();
@Override
public void add(Object object) {
// TODO Auto-generated method stub
vector.add(object);
}
@Override
public void remove(Object object) {
// TODO Auto-generated method stub
vector.remove(object);
}
@Override
public Iterator iterator() {
// TODO Auto-generated method stub
return new ConcreteIterator(vector);
}
}
Client 类:
import java.util.Vector;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
//使用java现有的Iterator迭代器来实现遍历元素
Vector vector = new Vector();
vector.add("a");
vector.add("b");
vector.add("c");
java.util.Iterator iterator = vector.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("--------------");
//使用我们定义的Iterator迭代器来实现遍历元素
ConcreteAggregate concreteAggregate = new ConcreteAggregate();
concreteAggregate.add("a");
concreteAggregate.add("b");
concreteAggregate.add("c");
Iterator iterator_02 = concreteAggregate.iterator();
while (iterator_02.hasNext()) {
System.out.println(iterator_02.next());
}
}
}
输出结果
a
b
c
--------------
a
b
c
Android源码中的模式实现
我们先查看一下Iterator类:
./libcore/luni/src/main/java/java/util/Iterator.java
public interface Iterator<E> {
/**
* Returns true if there is at least one more element, false otherwise.
* @see #next
*/
public boolean hasNext();
/**
* Returns the next object and advances the iterator.
*
* @return the next object.
* @throws NoSuchElementException
* if there are no more elements.
* @see #hasNext
*/
public E next();
/**
* Removes the last object returned by {@code next} from the collection.
* This method can only be called once between each call to {@code next}.
*
* @throws UnsupportedOperationException
* if removing is not supported by the collection being
* iterated.
* @throws IllegalStateException
* if {@code next} has not been called, or {@code remove} has
* already been called after the last call to {@code next}.
*/
public void remove();
}
我们可以看到Iterator,正如我们想的一样是一个接口,分别定义了三个方法:remove(),next(),hasNext()。
杂谈
因为java中,我们已经实现了java.util.Iterator接口,所有我们是经常用,但是都没有感觉到此设计模式。一般来说,在java开发中,我们不需要重新写一个Iterator的迭代,直接使用Collection下的实现容器类就可以了。
参考资料
(1).设计模式之禅—第20章 迭代器模式
(2)迭代器模式