• Java设计模式之迭代器模式


    迭代器模式(Iterator Pattern)是一种行为型设计模式,它提供一种方法来顺序访问一个容器对象中的各个元素,而无需暴露该容器对象的内部表示。迭代器模式将迭代逻辑封装在一个独立的迭代器对象中,使得可以在不暴露容器内部结构的情况下,通过迭代器按序访问容器中的元素。

    迭代器模式的主要参与者包括以下几个角色:

    1. 迭代器(Iterator):定义访问和遍历元素的接口,具备移动到下一个元素、获取当前元素等方法。
    2. 具体迭代器(Concrete Iterator):实现迭代器接口,负责实现具体的迭代逻辑,包括跟踪当前位置、移动到下一个元素、获取当前元素等操作。
    3. 容器(Container):定义创建迭代器的接口,可以是一个集合或聚合对象。
    4. 具体容器(Concrete Container):实现容器接口,负责创建具体迭代器对象。

    下面是一个简单的示例,展示了如何使用迭代器模式来遍历一个集合对象中的元素:

    // 迭代器接口
    interface Iterator {
        boolean hasNext();
        Object next();
    }
    
    // 具体迭代器实现
    class ConcreteIterator implements Iterator {
        private String[] collection;
        private int position = 0;
    
        public ConcreteIterator(String[] collection) {
            this.collection = collection;
        }
    
        public boolean hasNext() {
            return position < collection.length;
        }
    
        public Object next() {
            if (hasNext()) {
                return collection[position++];
            }
            return null;
        }
    }
    
    // 容器接口
    interface Container {
        Iterator createIterator();
    }
    
    // 具体容器实现
    class ConcreteContainer implements Container {
        private String[] collection;
    
        public ConcreteContainer(String[] collection) {
            this.collection = collection;
        }
    
        public Iterator createIterator() {
            return new ConcreteIterator(collection);
        }
    }
    
    // 使用迭代器遍历集合
    public class IteratorPatternExample {
        public static void main(String[] args) {
            String[] collection = { "Apple", "Banana", "Orange", "Grape" };
            Container container = new ConcreteContainer(collection);
            Iterator iterator = container.createIterator();
    
            while (iterator.hasNext()) {
                Object item = iterator.next();
                System.out.println(item);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    在上面的示例中,迭代器模式被用于遍历一个包含水果名称的字符串数组。具体迭代器实现类ConcreteIterator负责实现迭代逻辑,而具体容器实现类ConcreteContainer负责创建迭代器对象。通过调用容器的createIterator()方法,可以获取一个迭代器对象,然后使用该迭代器对象遍历容器中的元素。

    推荐一个ChatGPT使用渠道:点击直达
  • 相关阅读:
    docker使用记录1:构建java、python、c++环境镜像
    java和vue视频点播系统视频弹幕系统
    【线性代数】【一】 1.4 矩阵运算
    OIS、EIS原理
    Python抽象类和接口类
    R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、计算单个连续变量的分组汇总统计信息
    Python+大数据-hadoop(四)-Hadoop MapReduce、YARN、HA
    Python接口自动化测试post请求和get请求,获取请求返回值
    mmrotate学习(4):mmrotate框架训练数据集
    11. RBAC权限管理从零到一实现(二)
  • 原文地址:https://blog.csdn.net/kkwyting/article/details/133853492