设计模式-迭代器模式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());
        }
    }
}
相关推荐
砍光二叉树1 小时前
【设计模式】结构型-桥接模式
设计模式·桥接模式
姓蔡小朋友1 小时前
Agent Skill设计模式
开发语言·javascript·设计模式
砍光二叉树1 小时前
【设计模式】结构型-外观模式
设计模式·外观模式
zhaoshuzhaoshu2 小时前
设计模式6大原则详细对比(含场景举例)
设计模式·设计语言
砍光二叉树2 小时前
【设计模式】行为型-观察者模式
java·观察者模式·设计模式
泯仲16 小时前
Ragent项目7种设计模式深度解析:从源码看设计模式落地实践
java·算法·设计模式·agent
WarrenMondeville18 小时前
1.Unity面向对象-单一职责原则
unity·设计模式·c#
bmseven18 小时前
23种设计模式 - 适配器模式(Adapter)
设计模式·适配器模式
bmseven19 小时前
23种设计模式 - 组合模式(Composite)
设计模式·组合模式
MarkHD20 小时前
RPA工程化实践:三种核心设计模式让复杂流程优雅可控
linux·设计模式·rpa