当前位置: 首页 > news >正文

Java设计模式 —— 【行为型模式】迭代器模式(Iterator Pattern)详解

文章目录

  • 模式介绍
  • 优缺点
  • 适用场景
  • 案例实现


模式介绍

迭代器模式是一种行为模式,它提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需要知道集合对象的底层表示,即:不暴露其内部的结构

如果我们的集合元素是用不同的方式实现的,有数组,还有java的集合类,或者还有其他方式,当客户端要遍历这些集合元素的时候就要使用多种遍历方式(不统一),而且还会暴露元素的内部结构,就可以考虑使用迭代器模式解决。


优缺点

1、优点:

  • 支持多种遍历方式: 不同的迭代器可以定义不同的遍历方式;
  • 简化聚合类: 在原有的聚合对象中不需要再自行提供数据遍历等方法;
  • 隐藏了聚合的内部结构: 客户端要遍历聚合的时候只能取到迭代器,而不会知道聚合的具体组成;
  • 扩展性: 由于引入了抽象层,增加新的聚合类和迭代器类都很方便,无须修改原有代码,满足 “开闭原则” 的要求。

2、缺点:

  • 增加了类的个数,这在一定程度上增加了系统的复杂性。

适用场景

  • 当需要为聚合对象提供多种遍历方式时。
  • 当需要为遍历不同的聚合结构提供一个统一的接口时。
  • 当访问一个聚合对象的内容而无须暴露其内部细节的表示时。

案例实现

定义一个可以存储学生对象的容器对象,将遍历该容器的功能交由迭代器实现【抽象迭代器角色我们用JAVA内置的实现】

UML:
在这里插入图片描述
【抽象聚合角色】:

public interface StudentAggregate {
    //添加学生功能
    void addStudent(Student student);
    //获取迭代器对象功能
    Iterator getStudentIterator();
}

【具体聚合角色】:

public class StudentAggregateImpl implements StudentAggregate {
    private List<Student> list = new ArrayList<>();  // 学生列表

    @Override
    public void addStudent(Student student) {
        this.list.add(student);
    }

    @Override
    public Iterator<Student> getStudentIterator() {
        return new StudentIteratorImpl(list);
    }
}

【具体迭代器角色】:

public class StudentIteratorImpl implements Iterator<Student>{
    private List<Student> list;
    private int position = 0;

    public StudentIteratorImpl(List<Student> list) {
        this.list = list;
    }

    @Override
    public boolean hasNext() {
        return position < list.size();
    }

    @Override
    public Student next() {
        Student currentStudent = list.get(position);
        position ++;
        return currentStudent;
    }
}

测试:

public class Client {
    public static void main(String[] args) {
        StudentAggregateImpl aggregate = new StudentAggregateImpl();
        aggregate.addStudent(new Student("张三", "001"));
        aggregate.addStudent(new Student("李四", "002"));
        aggregate.addStudent(new Student("王五", "003"));
        aggregate.addStudent(new Student("赵六", "004"));
        aggregate.addStudent(new Student("田七", "005"));

        Iterator<Student> iterator = aggregate.getStudentIterator();
        while (iterator.hasNext()){
            Student student = iterator.next();
            System.out.println(student);
        }
    }
}

在这里插入图片描述

相关文章:

  • 我的ChatGPT怎么登不上?
  • CentOS7安装 FFmpeg
  • Self-Pro: A Self-Prompt and Tuning Framework for Graph Neural Networks
  • Spring Boot 监听器(Listeners)详细教程
  • 2024华为OD机试真题-热点网站统计(C++)-E卷-100分
  • AVM 环视拼接 鱼眼相机
  • 离散傅里叶变换(Discrete Fourier Transform, DFT)及其在图像处理中的应用
  • 动态表头报表的绘制与导出
  • 内网穿透的应用-企业级远程办公方案:NAS部署网页版Linux,HTTPS加密访问全配置
  • Ubuntu 创建新用户及设置权限
  • SSH远程登录并执行命令
  • 工程化与框架系列(10)--微前端架构
  • springboot使用redis
  • 前端请求乱序问题分析与AbortController、async/await、Promise.all等解决方案
  • 技术速递|增强 Razor 生产力的新功能!
  • Redis数据结构详解
  • Spring Boot 中 RabbitMQ 的使用
  • 【前端基础】3、HTML的常用元素(h、p、img、a、iframe、div、span)、不常用元素(strong、i、code、br)
  • (转)Java中collection和 collections区别
  • Linux切换Python版本
  • 中美会谈前都发生了什么?美方为何坐不住了?
  • 技术派|巴基斯坦导弹:让印度保持克制的“定海神针”?
  • 中美经贸高层会谈11日在日内瓦将继续进行
  • 上海发布首份直播电商行业自律公约,禁止虚假宣传、商业诋毁
  • 新买宝马竟是“维修车”,男子发视频维权被4S店索赔100万
  • 早期投资人蜂巧资本清仓泡泡玛特套现超22亿港元,称基金即将到期