• 设计模式——迭代器模式


    设计模式——迭代器模式

    定义: 提供一个对象来顺序访问聚合对象中的一系列数据且不暴露聚合对象的内部

    结构:

    1. 抽象聚合: 定义存储, 添加, 删除聚合元素以及创建迭代器对象的接口
    2. 具体聚合: 实现抽象聚合类, 返回一个具体迭代器的实例
    3. 抽象迭代器: 定义访问和遍历聚合元素的接口, 通常包含haseNext(), next()方法
    4. 具体迭代器: 实现抽象迭代器接口中所定义的方法, 完成对聚合对象的遍历, 记录遍历的当前位置

    实现

    抽象聚合

    public interface Aggregate<T> {
    
        void add(T obj);
    
        void remove(T obj);
    
        Iterator<T> getIterator();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    具体聚合

    @Data
    @AllArgsConstructor
    public class Student {
    
        private String name;
    
        private String number;
    
    }
    
    public class StudentAggregateImpl implements Aggregate<Student> {
    
        private List<Student> list = new ArrayList<Student>();
    
        public void add(Student obj) {
            list.add(obj);
        }
    
        public void remove(Student obj) {
            list.remove(obj);
        }
    
        public Iterator<Student> getIterator() {
            return new StudentIteratorImpl(list);
        }
    }
    
    • 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

    抽象迭代

    public interface Iterator<T> {
    
        boolean hasNext();
    
        T next();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    具体迭代

    public class StudentIteratorImpl implements Iterator<Student> {
    
        private List<Student> list;
    
        // 记录遍历时的位置
        private int position = 0;
    
        public StudentIteratorImpl(List<Student> list) {
            this.list = list;
        }
    
        public boolean hasNext() {
            return position < list.size();
        }
    
        public Student next() {
            return list.get(position++);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Main

    /**
     * 迭代器模式
     * 优点:
     * 1. 支持以不同方式遍历一个聚合对象, 同一个聚合对象可以定义多种遍历方式,
     * 迭代器模式中只需要换一个迭代器即可改变遍历算法
     * 2. 迭代器简化了聚合类, 引入了迭代器原有的聚合对象不需要再提供遍历方法
     * 3. 由于引入了抽象层, 增加聚合类和迭代器都方便, 不需要修改源代码
     *
     * 缺点:
     * 1. 增加类个数, 系统复杂性升高
     */
    public class Main {
        public static void main(String[] args) {
            Aggregate<Student> aggregate = new StudentAggregateImpl();
            aggregate.add(new Student("wtw", "123"));
            aggregate.add(new Student("pk", "456"));
            Iterator<Student> iterator = aggregate.getIterator();
            while(iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    使用场景:

    1. 需要为聚合对象提供多种遍历方式时
    2. 需要为不同的聚合对象提供一个统一的接口时
    3. 访问聚合对象但不需要暴露其内部细节时
  • 相关阅读:
    JavaSE入门---认识方法
    Dubbo和SpringCloud对比
    禁止linux shell 终端显示完整工作路径,如何让linux bash终端不显示当前工作路径
    集合java
    Python获取MP3文件的ID3标签信息
    Docker搭建RabbitMQ集群
    力扣(LeetCode)1678. 设计 Goal 解析器(C++)
    高精度的AI图像编辑: EditGAN
    LeetCode每日一题——2562. Find the Array Concatenation Value
    HTML5与CSS3实现动态网页(下)
  • 原文地址:https://blog.csdn.net/KobeSacre/article/details/126680354