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

23中设计模式-迭代器(Iterator)设计模式

迭代器设计模式

  • 🚩什么是迭代器设计模式?
  • 🚩迭代器设计模式的特点
  • 🚩迭代器设计模式的结构
  • 🚩迭代器设计模式的优缺点
  • 🚩迭代器设计模式的Java实现
  • 🚩代码总结
  • 🚩总结

🚩什么是迭代器设计模式?

迭代器设计模式(Iterator Pattern) 是一种 行为型设计模式,它提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。迭代器模式将遍历数据的职责从聚合对象中分离出来,使得聚合对象可以更专注于数据存储,而迭代器则专注于遍历数据。

使用场景

  • 访问一个聚合对象的内容而无需暴露其内部表示

  • 为聚合对象提供多种遍历方式

  • 为不同的聚合结构提供统一的遍历接口

  • 适用于 集合类树形结构图结构 等数据结构的遍历

🚩迭代器设计模式的特点

  • 分离集合对象的遍历行为:将遍历算法与集合对象分离

  • 多种遍历方式:可以为同一个聚合对象提供多种遍历方式

  • 统一的遍历接口:为不同的聚合结构提供统一的遍历接口

  • 简化聚合接口:聚合对象不需要提供多种遍历方法

  • 并行遍历:支持对同一聚合对象的多个同时遍历

🚩迭代器设计模式的结构

迭代器模式主要包含以下部分:

  • Iterator(迭代器接口):定义访问和遍历元素的接口

  • ConcreteIterator(具体迭代器):实现迭代器接口,跟踪当前访问位置

  • Aggregate(聚合接口):定义创建相应迭代器对象的接口

  • ConcreteAggregate(具体聚合):实现创建相应迭代器的接口

  • Client(客户端):通过迭代器遍历聚合对象

图例:

在这里插入图片描述

🚩迭代器设计模式的优缺点

✅ 优点

  • 支持多种遍历方式:可以同时使用不同的方式遍历同一个聚合

  • 简化聚合类:将遍历逻辑从聚合类中分离出来

  • 符合单一职责原则:将管理集合和遍历集合的职责分离

  • 符合开闭原则:可以新增迭代器而无需修改聚合类

  • 隐藏内部实现:客户端无需知道聚合的内部结构

❌ 缺点

  • 增加系统复杂度:对于简单的聚合可能过度设计

  • 性能开销:迭代器模式可能会带来一定的性能开销

  • 增加类的数量:每个聚合类都需要对应的迭代器类

🚩迭代器设计模式的Java实现

代码地址:GitHub

  • 创建 Book 类(聚合元素)
/**
 * @author hanson.huang
 * @version V1.0
 * @ClassName Book
 * @Description 书籍类 - 聚合中的元素
 * @date 2025/3/25 19:07
 **/
public class Book {

    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }
}
  • 创建 Iterator 接口
/**
 * @author hanson.huang
 * @version V1.0
 * @InterfaceName Iterator
 * @Description 迭代器接口
 * @date 2025/3/25 19:09
 **/
public interface Iterator {
    boolean hasNext();
    Book next();
}
  • 创建 Aggregate 接口
/**
 * @author hanson.huang
 * @version V1.0
 * @InterfaceName Aggregate
 * @Description 聚合接口
 * @date 2025/3/25 19:10
 **/
public interface Aggregate {
    Iterator createIterator();
}
  • 创建 BookAggregate 具体聚合类
/**
 * @author hanson.huang
 * @version V1.0
 * @ClassName BookAggregate
 * @Description 具体聚合类 - 书籍集合
 * @date 2025/3/25 19:10
 **/
public class BookAggregate implements Aggregate {

    private List<Book> list = new ArrayList<>();

    public void add(Book book) {
        list.add(book);
    }

    public Book get(int index) {
        return list.get(index);
    }

    public int getSize() {
        return list.size();
    }

    @Override
    public Iterator createIterator() {
        return new BookIterator(this);
    }
}
  • 创建 BookIterator 具体迭代器
/**
 * @author hanson.huang
 * @version V1.0
 * @ClassName BookIterator
 * @Description 具体迭代器
 * @date 2025/3/25 19:12
 **/
public class BookIterator implements Iterator {

    private int index;
    private BookAggregate bookAggregate;

    public BookIterator(BookAggregate bookAggregate) {
        this.index = 0;
        this.bookAggregate = bookAggregate;
    }

    @Override
    public boolean hasNext() {
        return index < bookAggregate.getSize();
    }

    @Override
    public Book next() {
        Book book = bookAggregate.get(index);
        index++;
        return book;
    }
}
  • 测试迭代器模式
/**
 * @author hanson.huang
 * @version V1.0
 * @ClassName IteratorPattern
 * @Description 测试迭代器模式
 * @date 2025/3/25 19:13
 **/
public class IteratorPattern {
    public static void main(String[] args) {
        BookAggregate bookAggregate = new BookAggregate();

        String[] books = {"数据结构", "操作系统", "计算机网络", "计算机组成原理"};
        double[] prices = {10.24, 20.48, 40.96, 81.92};

        for (int i = 0; i < books.length; i++) {
            bookAggregate.add(new Book(books[i], prices[i]));
        }
        Iterator iterator = bookAggregate.createIterator();
        while (iterator.hasNext()) {
            Book book = (Book) iterator.next();
            System.out.println(book.getName() + " " + book.getPrice());
        }
    }
}

📌 运行结果

在这里插入图片描述

🚩代码总结

  • Book 类表示聚合中的元素

  • Iterator 接口定义遍历操作

  • Aggregate 接口定义创建迭代器的方法

  • BookAggregate 是具体聚合类,管理Book对象的集合

  • BookIterator 是具体迭代器,实现遍历逻辑

  • IteratorPattern 客户端使用迭代器遍历聚合对象

🚩总结

  • 迭代器设计模式 提供了一种方法顺序访问聚合对象中的元素

  • 核心是 将遍历行为从聚合对象中分离出来,使两者可以独立变化

  • 适用于 需要遍历集合对象而又不暴露其内部结构 的场景

✅ Java源码中的应用场景:

  • Java集合框架

    • java.util.Iterator 接口

    • 所有集合类都实现了迭代器模式

创作不易,不妨点赞、收藏、关注支持一下,各位的支持就是我创作的最大动力❤️

在这里插入图片描述


http://www.kler.cn/a/610772.html

相关文章:

  • 【Git多分支使用教程】
  • Android 中 Activity 和 Fragment 的区别
  • C# 多标签浏览器 谷歌内核Csharp
  • 从底层原理到实际应用:BFS 算法借助队列征服迷宫
  • word写latex-Mathtype安装成功-方法
  • Linux 挂载磁盘操作指南
  • HTB 笔记 | SQL 注入基础:轻松有趣指南 | P1
  • JSON简介及C++中的JSON使用指南
  • Flutter 常见错误和坑
  • skynet.netpack四个核心函数详解
  • 机器学习都有哪些算法?
  • Golang 的 Waitgroup 锁用 Java的话要怎么实现?
  • LIMS应用的意义-LIMS厂家排名推荐
  • 全分辨率免ROOT懒人精灵-自动化编程思维-设计思路-实战训练
  • Docker镜像迁移方案
  • Rust从入门到精通之入门篇:3.Hello World
  • [问题收集]mysql主从分离过程中,数据不同步可能导致的后果以及应对策略
  • Ubuntu 重置密码方法
  • Android studio无法查看源码
  • 2.4 Gannt图【甘特图】