设计模式之迭代器模式

1.迭代器模式概念

用于顺序访问集合对象中的元素,而无需暴露底层的数据结构。这种模式提供了一种访问集合元素的方法,同时隐藏了集合内部的实现细节。

2.举个栗子

正常遍历和迭代器模式遍历模式,如下图

可以看出我们只要遍历迭代器对象,不关注集合本身,依旧可以获取元素,遍历元素

3.代码实现

1)Iterator接口代码

java 复制代码
package org.xiji.iterator;

/**
 * 定义迭代器的行为
 */
public interface Iterator<T> {
    /**
     *  判断下一个元素是否存在
     *
     */
    boolean hasNext();

    /**
     * 获取下一个元素
     *
     */
    T next();
    /**
     * 增加一个元素
     */
    void add(T t);

    /**
     * 减少末尾的怨怒是
     */
    void remove();

    /**
     * 通过索引移除元素
     */
    void remove(int index);
}

2)迭代器实现类

注:迭代器中的index变量相当于C中的指针,当我们遍历失败的时候需要重置为0,否则,下遍历该对象时还会从当前索引继续遍历

java 复制代码
package org.xiji.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * 定义自己的 列表迭代器
 */
public class MyList<T> implements Iterator<T> {
    /***
     * 定义遍历元素集合
     */
    private List<T> list = new ArrayList<T>();

    /**
     *
     * 定义起始变量
     */
    private int index=0;
    @Override
    public boolean hasNext() {


        if (this.index < list.size() || this.index<=0) {
            return true;
        }
        this.index = 0;//指针归零
        return false;
    }

    @Override
    public T next() {
        //如果遍历的元素在正常范围内继续遍历
        if(!hasNext()){
            throw new RuntimeException("指针异常");
        }
        return list.get(this.index++);
    }
    //为迭代器实现集合添加元素对象

    @Override
    public void add(T t) {
        this.list.add(t);
    }
    //移除迭代器最后对象

    @Override
    public void remove() {
        if (this.list.size() <= 0) {

            throw new RuntimeException("指针超出范围");
        }
        //移除最后一个元素
        this.list.remove(this.list.size() - 1);
        

    }
    //移除迭代器指定元素

    @Override
    public void remove(int index) {
        if (index < 0 || index >= this.list.size()) {
            throw new RuntimeException("指针超出范围");
        }
        this.list.remove(index);

    }
    

}

3)迭代器测试类

注:使用while循环实现,for循环上面已经给了

java 复制代码
package org.xiji.iterator;

/**
 * 迭代器测试类
 */
public class IteratorMain {

    public static void main(String[] args) {
        MyList<String> stringMyList = new MyList<>();
        //字符串迭代器类
        stringMyList.add("张三");
        stringMyList.add("李四");
        stringMyList.add("王五");
        while (stringMyList.hasNext()) {
            System.out.println(stringMyList.next());
        }
        System.out.println("------------------------------------------------------------- ");
        stringMyList.remove();
        while (stringMyList.hasNext()) {
            System.out.println(stringMyList.next());
        }
        System.out.println("-------------------------------------------------------------");
        stringMyList.add("小久");
        while (stringMyList.hasNext()) {
            System.out.println(stringMyList.next());
        }
        System.out.println("-------------------------------------------------------------");
        stringMyList.remove(1);
        while (stringMyList.hasNext()) {
            System.out.println(stringMyList.next());
        }
    }
}

4)运行结果

4.个人理解

感觉就是对集合的操作的二次封装,简化操作,不关心内部实现

相关推荐
karry_k7 小时前
MyBatis批量insert-select踩坑:useGeneratedKeys=true 可能让PostgreSQL返回大量插入结果
java·后端
karry_k7 小时前
PostgreSQL 在 MyBatis 中执行正常 SQL 失效:一次 DELETE USING 踩坑记录
java·后端
SamDeepThinking11 小时前
从源码到代码:MyBatis-Flex 与 MyBatis-Plus 的逐项对比
java·后端·程序员
她的男孩14 小时前
Spring Boot 接 Flowable 工作流:用 3 个注解搭一个请假审批流程
java·后端·架构
荣码16 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
plainGeekDev17 小时前
Gson → kotlinx.serialization
android·java·kotlin
小bo波1 天前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing
咖啡八杯1 天前
GoF设计模式——备忘录模式
java·后端·spring·设计模式