侧边栏壁纸
博主头像
人生短短几个秋

行动起来,活在当下

  • 累计撰写 45 篇文章
  • 累计创建 20 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

设计模式-迭代器模式

人生短短几个秋
2025-01-25 / 0 评论 / 0 点赞 / 15 阅读 / 0 字

设计模式 - 迭代器模式(Iterator Pattern)

介绍

迭代器模式是一种行为设计模式,它提供了一种方法来遍历集合内的元素,而无需暴露底层的表示。这种模式使得我们可以以一致的方式遍历不同的集合类型,而无需关心集合的具体实现细节。

实现

我们以一个简单的例子来说明如何使用迭代器模式来遍历不同类型的集合。

定义迭代器接口

首先定义一个迭代器接口,它声明了遍历集合的基本操作。

interface Iterator {
    boolean hasNext();
    Object next();
}

定义集合接口

接着定义一个集合接口,它声明了返回迭代器的方法。

interface Collection {
    Iterator iterator();
}

创建具体集合类

然后创建具体的集合类,这些类实现了 Collection 接口,并提供了具体的迭代器实现。

数组集合

class ArrayCollection implements Collection {
    private Object[] items;
    private int count = 0;

    public ArrayCollection(int capacity) {
        items = new Object[capacity];
    }

    public void add(Object item) {
        ensureCapacity();
        items[count++] = item;
    }

    private void ensureCapacity() {
        if (count == items.length) {
            Object[] newArray = new Object[items.length * 2];
            System.arraycopy(items, 0, newArray, 0, items.length);
            items = newArray;
        }
    }

    @Override
    public Iterator iterator() {
        return new ArrayIterator();
    }

    private class ArrayIterator implements Iterator {
        private int position = 0;

        @Override
        public boolean hasNext() {
            return position < count;
        }

        @Override
        public Object next() {
            if (hasNext()) {
                return items[position++];
            }
            return null;
        }
    }
}

链表集合

class LinkedList implements Collection {
    private Node head;
    private int count = 0;

    private static class Node {
        Object data;
        Node next;

        Node(Object data) {
            this.data = data;
            this.next = null;
        }
    }

    public void add(Object item) {
        Node newNode = new Node(item);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
        count++;
    }

    @Override
    public Iterator iterator() {
        return new LinkedListIterator();
    }

    private class LinkedListIterator implements Iterator {
        private Node current = head;

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Object next() {
            if (hasNext()) {
                Object item = current.data;
                current = current.next;
                return item;
            }
            return null;
        }
    }
}

使用迭代器模式

接下来,在主程序中使用迭代器模式来遍历不同的集合。

public class CollectionApp {
    public static void main(String[] args) {
        Collection arrayCollection = new ArrayCollection(5);
        Collection linkedList = new LinkedList();

        // 向数组集合添加元素
        arrayCollection.add("Apple");
        arrayCollection.add("Banana");
        arrayCollection.add("Cherry");

        // 向链表集合添加元素
        linkedList.add("Dog");
        linkedList.add("Elephant");
        linkedList.add("Fish");

        // 遍历数组集合
        System.out.println("Array Collection:");
        Iterator arrayIterator = arrayCollection.iterator();
        while (arrayIterator.hasNext()) {
            System.out.println(arrayIterator.next());
        }

        // 遍历链表集合
        System.out.println("\nLinked List:");
        Iterator linkIterator = linkedList.iterator();
        while (linkIterator.hasNext()) {
            System.out.println(linkIterator.next());
        }
    }
}

使用场景

迭代器模式适用于以下情况:

  • 当一个聚合对象想提供多种遍历其元素的方式。
  • 当遍历使用的算法应该独立于聚合对象的产品结构。
  • 当同一个聚合对象会被多个遍历者使用。

总结

迭代器模式通过提供一个统一的接口来遍历不同的集合,使得客户端代码无需关心集合的具体实现。这种方式不仅增强了代码的可维护性和可扩展性,还可以轻松支持多种遍历算法。


以上就是迭代器模式的一个实现案例,希望这篇文章能帮助你理解迭代器模式的使用方式及其优势。

0

评论区