架构师内功心法,非常熟知但并不知其所以然的迭代器模式详解

编程

迭代器模式(Iterator Pattern)又称为游标模式(Cursor Pattern),它提供一种顺序访问的集合或容器对象元素的方法,而无需暴露集合内部表示。迭代器模式可以为不同的容器提供一种遍历行为,而不用关心容器内元素组成结构。迭代器模式的本质是抽离集合对象迭代行为到迭代器中,提供一致访问接口。

一、迭代器模式的应用场景

迭代器模式在我们生活中应用的得也比较广泛,比如物流系统中的传送带,不管传送的是什么物品,都被打包成一个一个的箱子并且有一个统一的二维码。这样我们不需要关心箱子里面是啥,我们在分发时只需要一个一个检发送的目的地即可。再比如,我们平时乘坐交通工具都是统一刷卡或者刷脸进站,而不需要关心是男生还是女性、是残疾人还是正常人等个性化的信息。

我们把多个对象聚在一起形成的总体称之为集合(Aggregate),集合对象是能够包容一组对象的容器对象。不同集合其内部元素的聚和结构可能不同,而迭代器模式屏蔽了内部元素获取细节,为外部提供一致的元素访问行为,解耦了元素迭代与集合对象间的耦合,并且提供不同的迭代器,可以为同一个对象提供不同顺序的元素访问行为,扩展了集合对元素迭代功能,符合开闭原则。

迭代器模式使用于以下几个场景:

  • 访问一个集合对象的内容而无需暴露它的内部表示;
  • 为遍历不同的集合结构提供一个统一的访问接口。

迭代器模式主要包括四种角色:

  • 抽象迭代器(Iterator):负责定义访问和遍历元素的接口;
  • 具体迭代器(ConcreteIterator):提供具体的元素遍历行为;
  • 抽象容器(Aggregate):负责定义提供具体迭代器的接口;
  • 具体容器(ConcreteAggregate):创建具体迭代器。

1.1 自定义的迭代器

来以课程为例,创建一个课程的集合,集合中的每一个元素都是课程对象,然后自定义一个迭代器,将集合中的元素每一个课程对象信息读取出来。首先创建集合元素课程对象Course 类:

public class Course {

private String name;

public Course(String name) {

this.name = name;

}

public String getName() {

return name;

}

}

然后创建自定义接口Iterator:

public interface Iterator<E> {

E next();

boolean hasNext();

}

接着创建课程集合CourseAggregate接口:

public interface CourseAggregate {

void add(Course course);

void remove(Course course);

Iterator<Course> iterator();

}

然后实现迭代器接口和课程结合接口:

public class IteratorImpl<E> implements Iterator<E> {

private List<E> list;

private int cursor;

private E element;

public IteratorImpl(List<E> list) {

this.list = list;

}

@Override

public E next() {

System.out.println("当前位置是:" + cursor);

element = list.get(cursor);

cursor ++ ;

return element;

}

@Override

public boolean hasNext() {

if(cursor > list.size() - 1) {

return false;

}

return true;

}

}

课程集合实现类CourseAggregateImpl:

public class CourseAggregateImpl implements CourseAggregate {

private List list;

public CourseAggregateImpl(List list) {

this.list = new ArrayList();

}

@Override

public void add(Course course) {

list.add(course);

}

@Override

public void remove(Course course) {

list.remove(course);

}

@Override

public Iterator<Course> iterator() {

return new IteratorImpl<Course>(list);

}

}

测试main方法:

public static void main(String[] args) {

Course chinese = new Course("语文");

Course math = new Course("数学");

Course english = new Course("英语");

CourseAggregate courseAggregate = new CourseAggregateImpl();

courseAggregate.add(chinese);

courseAggregate.add(math);

courseAggregate.add(english);

courseAggregate.remove(math);

Iterator<Course> courseIterator = courseAggregate.iterator();

while (courseIterator.hasNext()) {

System.out.println(courseIterator.next().getName());

}

}

二、迭代器模式在源码中的体现

2.1 JDK中的迭代器Iterator

Iterator接口源码如下:

public interface Iterator<E> {

/**

* Returns {@code true} if the iteration has more elements.

* (In other words, returns {@code true} if {@link #next} would

* return an element rather than throwing an exception.)

*

* @return {@code true} if the iteration has more elements

*/

boolean hasNext();

/**

* Returns the next element in the iteration.

*

* @return the next element in the iteration

* @throws NoSuchElementException if the iteration has no more elements

*/

E next();

/**

* Removes from the underlying collection the last element returned

* by this iterator (optional operation). This method can be called

* only once per call to {@link #next}. The behavior of an iterator

* is unspecified if the underlying collection is modified while the

* iteration is in progress in any way other than by calling this

* method.

*

* @implSpec

* The default implementation throws an instance of

* {@link UnsupportedOperationException} and performs no other action.

*

* @throws UnsupportedOperationException if the {@code remove}

* operation is not supported by this iterator

*

* @throws IllegalStateException if the {@code next} method has not

* yet been called, or the {@code remove} method has already

* been called after the last call to the {@code next}

* method

*/

default void remove() {

throw new UnsupportedOperationException("remove");

}

/**

* Performs the given action for each remaining element until all elements

* have been processed or the action throws an exception. Actions are

* performed in the order of iteration, if that order is specified.

* Exceptions thrown by the action are relayed to the caller.

*

* @implSpec

* <p>The default implementation behaves as if:

* <pre>{@code

* while (hasNext())

* action.accept(next());

* }</pre>

*

* @param action The action to be performed for each element

* @throws NullPointerException if the specified action is null

* @since 1.8

*/

default void forEachRemaining(Consumer<? super E> action) {

Objects.requireNonNull(action);

while (hasNext())

action.accept(next());

}

}

从上面代码中,我们看到两个主要的方法定义hasNext()和next()方法,和我们自己写的完全一致。另外,从上面的代码中,我们看到remove()方法实现似曾相识。其实是在组合模式中我们见到过。迭代器模式和组合模式,两者似乎存在一定的相似性。组合模式解决的是统一树形结构各层次访问接囗,迭代器模式解决的是统一各集合对象元素遍历接囗。虽然他们的适配场景不同,但核心理念是相通的。

Iterator接口的实现类,在ArrayList类中的内部实现类Itr,它实现了Iterator接口:

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();

}

}

其中的next()和hasNext()方法的实现都很简单,ArrayList类中还有几个迭代器对Itr进行了进一步的扩展,来看下ListItr类的源码:

private class ListItr extends Itr implements ListIterator<E> {

ListItr(int index) {

super();

cursor = index;

}

public boolean hasPrevious() {

return cursor != 0;

}

public int nextIndex() {

return cursor;

}

public int previousIndex() {

return cursor - 1;

}

@SuppressWarnings("unchecked")

public E previous() {

checkForComodification();

int i = cursor - 1;

if (i < 0)

throw new NoSuchElementException();

Object[] elementData = ArrayList.this.elementData;

if (i >= elementData.length)

throw new ConcurrentModificationException();

cursor = i;

return (E) elementData[lastRet = i];

}

public void set(E e) {

if (lastRet < 0)

throw new IllegalStateException();

checkForComodification();

try {

ArrayList.this.set(lastRet, e);

} catch (IndexOutOfBoundsException ex) {

throw new ConcurrentModificationException();

}

}

public void add(E e) {

checkForComodification();

try {

int i = cursor;

ArrayList.this.add(i, e);

cursor = i + 1;

lastRet = -1;

expectedModCount = modCount;

} catch (IndexOutOfBoundsException ex) {

throw new ConcurrentModificationException();

}

}

}

三、迭代器模式的优缺点

优点:

  • 多态迭代:为不同的聚合结构提供一致的遍历接囗,即一个迭代接口可以访问不同的集合对象;
  • 简化集合对象接囗:迭代器模式将集合对象本身应该提供的元素迭代接囗抽取到了迭代器中使集合对象无须关心具体迭代行为;
  • 元素迭代功能多样化:每个集合对象都可以提供一个或多个不同的迭代器,使的同种元素聚合结构可以有不同的迭代行为;
  • 解耦迭代与集合:迭代器模式封装了具体的迭代算法,迭代算法的变化,不会影响到集合对象的架构

缺点:

  • 对于比较简单的遍历(数组或者有序列表),使用迭代器方式遍历较为繁琐。

以上是 架构师内功心法,非常熟知但并不知其所以然的迭代器模式详解 的全部内容, 来源链接: utcz.com/z/514535.html

回到顶部