设计模式-迭代器模式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());
        }
    }
}
相关推荐
yunhuibin4 小时前
DP学习——简单工厂模式
设计模式·简单工厂模式
java小郭4 小时前
设计模式之责任链模式
设计模式·责任链模式
CodeLinghu5 小时前
【设计模式】策略模式(定义 | 特点 | Demo入门讲解)
设计模式·bash·策略模式
繁星十年9 小时前
《C++20设计模式》命令模式思考
设计模式·命令模式·c++20
明戈戈17 小时前
设计模式-模板方法模式
设计模式·模板方法模式
python资深爱好者17 小时前
在什么情况下你会使用设计模式
设计模式
PingCAP1 天前
Dify + TiDB Vector,快速构建你的AI Agent
数据库·人工智能·设计模式
BoldExplorer1 天前
设计模式(四)责任链模式
设计模式·责任链模式
GIS_JH1 天前
设计模式简单示例
设计模式
搬砖的小熊猫1 天前
设计模式探索:策略模式
设计模式·策略模式