码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【图解设计模式】迭代器模式


    Iterator模式用于在数据集合中按照顺序遍历集合。

    实例程序:将book放置到书架上,并按顺序显示

     

    1. public interface Aggregate {
    2. public abstract Iterator iterator();
    3. }
    1. public class Book {
    2. private String name;
    3. public Book(String name){
    4. this.name=name;
    5. }
    6. public String getName(){
    7. return name;
    8. }
    9. }
    1. public class BookShelf implements Aggregate {
    2. private Book[]books;
    3. private int last=0;
    4. public BookShelf(int maxSize){
    5. this.books=new Book[maxSize];
    6. }
    7. public Book getBookAt(int index){
    8. return books[index];
    9. }
    10. public void appendBook(Book book){
    11. this.books[last]=book;
    12. last++;
    13. }
    14. public int getLength(){
    15. return last;
    16. }
    17. @Override
    18. public Iterator iterator(){
    19. return new BookShelfIterator(this);
    20. }
    21. }
    1. public class BookShelfIterator implements Iterator {
    2. private BookShelf bookShelf;
    3. private int index;
    4. public BookShelfIterator(BookShelf bookShelf){
    5. this.bookShelf=bookShelf;
    6. this.index=0;
    7. }
    8. @Override
    9. public boolean hasNext(){
    10. if(indexreturn true;
    11. else return false;}
    12. @Override
    13. public Object next(){
    14. Book book = bookShelf.getBookAt(index);
    15. index++;
    16. return book;
    17. }
    18. }
    1. public interface Iterator {
    2. public abstract boolean hasNext();
    3. public abstract Object next();
    4. }
    1. public class Main {
    2. public static void main(String[] args) {
    3. BookShelf bookShelf=new BookShelf(4);
    4. bookShelf.appendBook(new Book("a"));
    5. bookShelf.appendBook(new Book("b"));
    6. bookShelf.appendBook(new Book("c"));
    7. Iterator it=bookShelf.iterator();
    8. while(it.hasNext()){
    9. Book book =(Book)it.next();
    10. System.out.println(book.getName());
    11. }
    12. }

    【角色说明】

    【Iterator】定义按顺序逐个遍历元素的接口(API)

    【ConcreteIterator】负责实现Iterator角色所定义的接口。该角色中包含了遍历集合所必需的信息

    【Aggregate】负责定义创建Iterator角色的接口。里面定义了iterator方法

    【ConcreteAggregate】集合

     引入Iterator后可以将遍历和实现分离开

     这里只使用了Iterator的hasNext方法和next方法,并没有调用BookShelf的方法,就是说不依赖集合的实现。只要具体的集合的iterator方法能正确的返回Iterator实例,不对while做修改也可以。

    “可复用”。

    “面对接口编程”

    【next方法】返回当前元素,并指向下一个元素

    【hasNext方法】确认接下来是否可以调用next方法。

    【迭代器的种类多种多样】遍历方法可以从前向后,从后向前或者跳跃式遍历

    【不需要deleteIterator】java中,没有被使用的对象实例会被自动删除。

    手写迭代器模式的注意点 

  • 相关阅读:
    linux查询java进程的指令,查询内存的指令
    下拉菜单选择播放视频后如何更改video标签
    实习打怪之路:webpack概念【入口、输出、装载机、插件、模块】(引自官网)
    SQL的函数
    [附源码]JAVA毕业设计高校疫情管理(系统+LW)
    网安周报|Mixin Network 云服务商数据库遭到攻击,涉案金额约 2 亿美元
    HTML (总结黑马的)
    力扣第39题 组合总和 c++ 回溯剪枝题
    C++原子变量
    《安富莱嵌入式周报》第284期:Matlab2022b发布,支持从 .NET 调用,耳机放大器,牛屎芯片替换,JSON可视化,开源的飞行软件和嵌入式系统框架
  • 原文地址:https://blog.csdn.net/m0_52043808/article/details/126721471
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号