设计模式之迭代器模式

迭代器模式(Iterator)

定义

提供一种顺序访问一个聚合对象(容器)中各个元素的方法,而又不暴露其内部的表示

使用场景

主要角色

  1. 迭代器Iterator
  2. 具体的迭代器ConcreteIterator
  3. 聚合Aggregate
  4. 具体的聚合ConcreteAggregate

类图

image-20240108094349328

ArrayList真实的继承体系

image-20240108094631124

示例代码

public interface Iterator<E> {
    
    boolean hasNext();
    
    E next();
    
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }

    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

java.util.ArrayList.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;

    Itr() {}

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

java.util.ArrayList#iterator

public Iterator<E> iterator() {
    return new Itr();
}

工作中遇到场景

相关推荐

  1. 设计模式模式

    2024-03-10 22:36:02       12 阅读
  2. 设计模式模式

    2024-03-10 22:36:02       8 阅读
  3. 【前端设计模式模式

    2024-03-10 22:36:02       37 阅读
  4. 【软件设计模式与组合模式

    2024-03-10 22:36:02       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 22:36:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 22:36:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 22:36:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 22:36:02       20 阅读

热门阅读

  1. for-in,for-of

    2024-03-10 22:36:02       20 阅读
  2. GPT的磁盘管理

    2024-03-10 22:36:02       23 阅读
  3. volatile

    2024-03-10 22:36:02       21 阅读
  4. HTML5- 拖拽功能

    2024-03-10 22:36:02       20 阅读
  5. Flask基于配置文件添加项目config配置

    2024-03-10 22:36:02       19 阅读
  6. 深入了解 Python 的 compile() 函数

    2024-03-10 22:36:02       20 阅读
  7. python中def一个方法,就一定对应一个return吗

    2024-03-10 22:36:02       19 阅读
  8. AI辅助研发

    2024-03-10 22:36:02       23 阅读
  9. 2022护网面试题总结

    2024-03-10 22:36:02       17 阅读