原文: https://howtodoinjava.com/design-patterns/behavioral/iterator-design-pattern/

根据 GoF 定义,迭代器模式提供了一种顺序访问聚合对象元素的方法,而无需暴露其基础表示。 这是行为设计模式

顾名思义,迭代器有助于以定义的方式遍历对象的集合,这对客户端应用很有用。 在迭代期间,客户端程序可以根据需要对元素执行各种其他操作。

1.何时使用迭代器设计模式

每种编程语言都支持一些数据结构,例如列表或映射,用于存储一组相关对象。 在 Java 中,我们具有ListMapSet接口及其实现,例如ArrayListHashMap

集合仅在提供一种在不暴露其内部结构的情况下访问其元素的方式时才有用。 迭代器承担此责任。

因此,任何时候,我们都有对象集合,并且客户端需要一种以某种适当顺序迭代每个集合元素的方法,我们必须使用迭代器模式来设计解决方案。

迭代器模式使我们能够以以下方式设计集合迭代器:

  • 我们能够访问集合的元素而无需暴露元素的内部结构或集合本身。
  • 迭代器支持从开始到结束以向前,向后或双向双向遍历一个集合。
  • 迭代器提供了一个统一的接口,用于透明地遍历不同的集合类型。

关键思想是从聚合对象中承担访问和遍历的责任,并将其放入定义标准遍历协议的Iterator对象中。

2.迭代器模式的真实示例

  • 在 Java 中,我们具有java.util.Iterator接口及其特定的实现,例如ListIterator。 所有 Java 集合都提供Iterator接口的一些内部实现,这些接口用于遍历集合元素。

    1. List<String> names = Arrays.asList("alex", "brian", "charles");
    2. Iterator<String> namesIterator = names.iterator();
    3. while (namesIterator.hasNext())
    4. {
    5. String currentName = namesIterator.next();
    6. System.out.println(currentName);
    7. }
  • 在媒体播放器中,我们列出了歌曲列表,我们可以通过遍历歌曲列表并选择所需的歌曲来播放歌曲。 这也是一个迭代器示例。

3.迭代器设计模式

3.1 架构

迭代器设计模式 - 图1

迭代器模式类图

3.2 设计参与者

迭代器模式的参与者如下:

  • Iterator:访问或遍历元素集合的接口。 提供具体的迭代器必须实现的方法。
  • ConcreteIterator:实现Iterator接口方法。 它还可以在遍历聚合集合时跟踪当前位置。
  • Aggregate:通常是一个集合接口,它定义可以创建Iterator对象的方法。
  • ConcreteAggregate:它实现Aggregate接口,其特定方法返回ConcreteIterator的实例。

4.迭代器设计模式示例

在此迭代器模式示例中,我们正在创建一个集合,该集合可以保存Token类的实例,并将提供一个迭代器来迭代序列中的标记集合。

  1. public class Topic
  2. {
  3. private String name;
  4. public Topic(String name) {
  5. super();
  6. this.name = name;
  7. }
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. }
  1. public interface Iterator<E>
  2. {
  3. void reset(); // reset to the first element
  4. E next(); // To get the next element
  5. E currentItem(); // To retrieve the current element
  6. boolean hasNext(); // To check whether there is any next element or not.
  7. }
  1. public class TopicIterator implements Iterator<Topic> {
  2. private Topic[] topics;
  3. private int position;
  4. public TopicIterator(Topic[] topics)
  5. {
  6. this.topics = topics;
  7. position = 0;
  8. }
  9. @Override
  10. public void reset() {
  11. position = 0;
  12. }
  13. @Override
  14. public Topic next() {
  15. return topics[position++];
  16. }
  17. @Override
  18. public Topic currentItem() {
  19. return topics[position];
  20. }
  21. @Override
  22. public boolean hasNext() {
  23. if(position >= topics.length)
  24. return false;
  25. return true;
  26. }
  27. }
  1. public interface List<E>
  2. {
  3. Iterator<E> iterator();
  4. }
  1. public class TopicList implements List<Topic>
  2. {
  3. private Topic[] topics;
  4. public TopicList(Topic[] topics)
  5. {
  6. this.topics = topics;
  7. }
  8. @Override
  9. public Iterator<Topic> iterator() {
  10. return new TopicIterator(topics);
  11. }
  12. }

使用迭代器的客户端代码将像这样。

  1. public class Main
  2. {
  3. public static void main(String[] args)
  4. {
  5. Topic[] topics = new Topic[5];
  6. topics[0] = new Topic("topic1");
  7. topics[1] = new Topic("topic2");
  8. topics[2] = new Topic("topic3");
  9. topics[3] = new Topic("topic4");
  10. topics[4] = new Topic("topic5");
  11. List<Topic> list = new TopicList(topics);
  12. Iterator<Topic> iterator = list.iterator();
  13. while(iterator.hasNext()) {
  14. Topic currentTopic = iterator.next();
  15. System.out.println(currentTopic.getName());
  16. }
  17. }
  18. }

程序输出。

  1. topic1
  2. topic2
  3. topic3
  4. topic4
  5. topic5

在评论中向我发送有关迭代器模式的问题。

学习愉快!

参考:维基百科