Java设计模式-迭代器模式Iterator

在这里插入图片描述

介绍

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

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

原理类图

在这里插入图片描述
迭代器模式的角色说明如下:

  • Iterator:访问或遍历元素集合的接口。提供具体迭代器必须实现的方法。

  • ConcreteIterator:实现Iterator接口方法。它还可以跟踪聚合集合遍历中的当前位置。

  • Aggregate:它通常是一个集合接口,它定义了一个可以创建迭代器对象的方法。

  • ConcreteAggregate:它实现Aggregate接口,其特定方法返回- -ConcreteIterator的实例。

什么时候使用?

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

只有当集合提供了访问其元素而不暴露其内部结构的方法时,它才有用。迭代器承担这一责任。

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

迭代器模式允许我们以如下方式设计集合迭代器:

  • 我们能够访问集合的元素,而不暴露元素的内部结构或集合本身。
  1. 迭代器支持在向前、向后或两个方向上从开始到结束对集合进行多次同时遍历。

  2. 迭代器为透明地遍历不同的集合类型提供了统一的接口。

JDK中的实现

在Java中,我们有Java.util.Iterator接口及其特定的实现,例如ListIterator。所有Java集合都提供了Iterator接口的一些内部实现,该接口用于迭代集合元素。

List<String> names = Arrays.asList("alex", "brian", "charles");
       
  Iterator<String> namesIterator = names.iterator();
   
  while (namesIterator.hasNext()) 
  {
    String currentName = namesIterator.next();
     
    System.out.println(currentName);
}

实战案例

在这个迭代器模式示例中,我们正在创建一个集合,该集合可以保存Topic类的实例,并将提供一个迭代器来按顺序迭代Topic集合。

public class Topic 
{
  private String name;
   
  public Topic(String name) {
    super();
    this.name = name;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
}
public interface Iterator<E> 
{
  void reset(); // reset to the first element
 
  E next(); // To get the next element
 
  E currentItem();  // To retrieve the current element
 
  boolean hasNext();  // To check whether there is any next element or not.
}
public class TopicIterator implements Iterator<Topic> {
   
  private Topic[] topics;
    private int position;
     
    public TopicIterator(Topic[] topics)
    {
        this.topics = topics;
        position = 0;
    }
 
  @Override
  public void reset() {
    position = 0;
  }
 
  @Override
  public Topic next() { 
    return topics[position++];
  }
 
  @Override
  public Topic currentItem() {
    return topics[position];
  }
 
  @Override
  public boolean hasNext() {
    if(position >= topics.length)
            return false;
        return true;
  }
}
public interface List<E>
{
  Iterator<E> iterator();
}
public class TopicList implements List<Topic>
{
  private Topic[] topics;
   
    public TopicList(Topic[] topics)
    {
        this.topics = topics;
    }
     
  @Override
  public Iterator<Topic> iterator() {
    return new TopicIterator(topics);
  }
}
public class Main 
{
  public static void main(String[] args) 
  {
    Topic[] topics = new Topic[5];
    topics[0] = new Topic("topic1");
    topics[1] = new Topic("topic2");
    topics[2] = new Topic("topic3");
    topics[3] = new Topic("topic4");
    topics[4] = new Topic("topic5");
     
    List<Topic> list = new TopicList(topics);
     
    Iterator<Topic> iterator = list.iterator();
     
    while(iterator.hasNext()) {
      Topic currentTopic = iterator.next();
      System.out.println(currentTopic.getName());
    }
  }
}
  • 程序输出:
    在这里插入图片描述
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值