设计模式之迭代器模式

小明 2025-04-28 17:56:32 2

迭代器模式(Iterator)

定义

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

使用场景

主要角色

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

类图

ArrayList真实的继承体系

示例代码

public interface Iterator {
    
    boolean hasNext();
    
    E next();
    
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    default void forEachRemaining(Consumer
The End
微信