设计模式-迭代器模式Iterator(行为型)

迭代器模式

迭代器模式属于行为型模式,用来遍历容器的一种模式。

图解

角色

  1. 迭代器接口:定义hasNext方法和next方法
  2. 具体迭代器:实现hasNext方法和next方法
  3. 容器接口:定义返回一个迭代器的接口
  4. 具体容器:实现返回迭代器接口

案例

该案例以简单实现ArrayList和LinkedList来演示迭代器模式。整体思想是,不管是什么容器,必须实现获取迭代器的方法,和定义一个迭代器来遍历对应的容器。

java 复制代码
public interface Iterable_<E> {
    Iterator_<E> iterator();
}

public interface Collection_<E> extends Iterable_<E>{
    void add(E e);
    int size();
}

public interface Iterator_<E> {
    boolean hasNest();
    E next();
}

ArrayList实现类

java 复制代码
public class ArrayList_<E> implements Collection_<E>{
    private static final int initSize = 10;
    private Object [] objects = new Object[initSize];
    private int index = 0;

    @Override
    public void add(E e) {
        if(index >= objects.length){
            Object[] newObjects = new Object[(int) (objects.length * 1.5)];
            System.arraycopy(objects,0,newObjects,0,newObjects.length);
            this.objects = newObjects;

        }else {
            objects[index] = e;
        }
        index++;
    }

    @Override
    public int size() {
        return index;
    }

    @Override
    public Iterator_<E> iterator() {
        return new ArrayIterator();
    }
    /** arrayList迭代器*/
    private class ArrayIterator<E> implements Iterator_<E>{
        int cursor;

        @Override
        public boolean hasNest() {
            return cursor != index;
        }
        @Override
        public E next() {
            E e= (E) objects[cursor];
            cursor ++;
            return e;
        }
    }
}

linkedList实现类

java 复制代码
public class LinkedList_<E> implements Collection_<E>{
    private int size = 0;
    private Node<E> lastNode = null;
    private Node<E> handNode = null;
    @Override
    public void add(E e) {
        Node<E> objectNode =new Node<>(e,null);
        if(lastNode == null){
            lastNode = objectNode;
            handNode = objectNode;
        }else {
            lastNode.next = objectNode;
            lastNode = objectNode;
        }
        this.size++;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator_<E> iterator() {
        return new LinkedIterator();
    }

    private class LinkedIterator implements Iterator_<E>{
        int cursor = 0;
        Node<E> tempNode = null;

        @Override
        public boolean hasNest() {
            return cursor != size;
        }

        @Override
        public E next() {
            cursor++;
            if(cursor ==1){
                tempNode = handNode;
                return handNode.getValue();
            }else {
                return tempNode.next.getValue();
            }


        }
    }
    private class Node<E>{
        private E value;
        private Node<E> next;

        public Node(E value, Node<E> next) {
            this.value = value;
            this.next = next;
        }

        public void setNext(Node<E> next) {
            this.next = next;
        }

        public E getValue() {
            return value;
        }
    }
}

Test类

java 复制代码
package com.shaoby.designpatterns.Iterator;

public class Test {
    public static void main(String[] args) {
        Collection_<String> objectArrayList_ = new LinkedList_<>();
        objectArrayList_.add("111");
        objectArrayList_.add("222");
        objectArrayList_.add("333");
        Iterator_<String> iterator = objectArrayList_.iterator();
        while (iterator.hasNest()){
            System.out.println(iterator.next());
        }
    }
}
相关推荐
feasibility.34 分钟前
AI 编程助手进阶指南:从 Claude Code 到 OpenCode 的工程化经验总结
人工智能·经验分享·设计模式·自动化·agi·skills·opencode
BD_Marathon1 小时前
七大设计原则介绍
设计模式
YigAin3 小时前
Unity23种设计模式之 享元模式
设计模式·享元模式
范纹杉想快点毕业17 小时前
实战级ZYNQ中断状态机FIFO设计
java·开发语言·驱动开发·设计模式·架构·mfc
茂桑21 小时前
DDD领域驱动设计-基础设施层
设计模式·架构
小温冲冲1 天前
通俗且全面精讲工厂设计模式
设计模式
进击的小头1 天前
设计模式与C语言高级特性的结合
c语言·设计模式
小温冲冲1 天前
通俗且全面精讲单例设计模式
开发语言·javascript·设计模式
Vivienne_ChenW1 天前
DDD领域模型在项目中的实战
java·开发语言·后端·设计模式
sg_knight1 天前
原型模式(Prototype)
python·设计模式·开发·原型模式