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


    设计模式——迭代器模式

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

    结构:

    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. 访问聚合对象但不需要暴露其内部细节时
  • 相关阅读:
    用Python如何进行Web开发
    springboot(ssm 经方药食两用服务平台 Java(code&LW)
    AI绘图之Midjourney初体验
    【微信小程序】后台数据交互于WX文件使用
    electron使用electron-builder macOS windows 打包 签名 更新 上架
    关于BigInteger和BigDecimal
    有2023最新的批量混剪软件的排名榜单吗?
    基于Google Gemini 探索大语言模型在医学领域应用评估和前景
    力扣(83.643)补8.29
    数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
  • 原文地址:https://blog.csdn.net/KobeSacre/article/details/126680354