• 设计模式-行为型-迭代器模式


    描述

    • 提供一个对象来顺序访问聚合列表中的元素,不暴露聚合列表中对象的内部表示。
    • 遍历单例集合时,经常使用到(Collection.iterator)。

    角色

    • 抽象聚合角色:存储,添加,删除聚合原始的抽象方法。
    • 具体聚合角色:集合,容器。实现抽象聚合角色,并返回具体迭代器实例。
    • 抽象迭代器角色:定义遍历聚合元素的接口,通常包括hasNest(),next()。
    • 具体迭代器角色:实现抽象迭代器角色,完成聚合对象的遍历。

    实现

    public class Test {
        public static void main(String[] args) {
            Peoples peoples = new Peoples();
            peoples.add(new People("阿椰", 25));
            peoples.add(new People("阿明", 22));
            peoples.add(new People("阿发", 23));
            MyIterator iterator = peoples.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }
    }
    
    class People {
        private String name;
        private Integer age;
    
        People(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    // 抽象聚合角色
    interface AbstractPeoples {
        void add(People people);
    
        void remove(People people);
    
        MyIterator iterator();
    }
    
    // 具体聚合角色
    class Peoples implements AbstractPeoples {
        private final List<People> peopleList = new ArrayList<>();
    
        @Override
        public void add(People people) {
            peopleList.add(people);
        }
    
        @Override
        public void remove(People people) {
            peopleList.remove(people);
        }
    
        @Override
        public MyIterator iterator() {
            return new PeopleIterator(peopleList);
        }
    }
    
    // 抽象迭代器角色
    interface MyIterator {
        boolean hasNext();
    
        People next();
    }
    
    // 具体迭代器角色
    class PeopleIterator implements MyIterator {
        // 指针
        private int point = 0;
        private List<People> peopleList;
    
        PeopleIterator(List<People> peopleList) {
            this.peopleList = peopleList;
        }
    
        @Override
        public boolean hasNext() {
            return peopleList.size() > point;
        }
    
        @Override
        public People next() {
            return peopleList.get(point++);
        }
    }
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    优点

    • 支持不同的方式(定义不同的具体迭代角色)遍历聚合对象。
    • 遍历方式和聚合角色松耦合,满足开闭原则(新增迭代器,不改原有代码)。

    缺点

    • 增加类数据,也就增加了系统的复杂度。

    使用场景

    • 当聚合对象需要切换或拥有多种遍历方式时,可以使用迭代器模式。
    • 当要为不同的聚合对象,提供一个统一的遍历方式时,可以使用迭代器模式。
    • 当遍历聚合对象,且要隐藏聚合对象中对象的细节时,可以使用迭代器模式。

    JDK中的应用

    • 单列集合,如 List
    • 抽象聚合角色:List
    • 具体聚合角色:ArrayList,LinkedList等
    • 抽象迭代器角色:Iterator
    • 具体迭代器角色:list.iterator() =》 Ite类
      具体迭代器角色 Itr
      在这里插入图片描述

    JAVA中迭代器模式的使用

    • 我们要使用迭代器模式的话,可以让我们定义的容器去实现java.util.Iterable接口,并重写iterator()方法,返回Iterable实现类即可。
    • 如下:
    public class Test {
        public static void main(String[] args) {
            PeopleList peopleList = new PeopleList();
            peopleList.add(new People("阿椰", 25));
            peopleList.add(new People("阿明", 22));
            peopleList.add(new People("阿发", 23));
            Iterator iterator = peopleList.jdkIterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }
    }
    
    // 具体聚合角色
    class PeopleList implements AbstractPeoples {
        private final List<People> peopleList = new ArrayList<>();
    
        @Override
        public void add(People people) {
            peopleList.add(people);
        }
    
        @Override
        public void remove(People people) {
            peopleList.remove(people);
        }
    
        @Override
        public MyIterator iterator() {
            return new PeopleIterator(peopleList);
        }
    
        public Iterator jdkIterator() {
            return new PeopleItr(peopleList);
        }
    
        private class PeopleItr implements Iterator<People> {
            // 指针
            private int point = 0;
            private List<People> peopleList;
    
            PeopleItr(List<People> peopleList) {
                this.peopleList = peopleList;
            }
    
            @Override
            public boolean hasNext() {
                return peopleList.size() > point;
            }
    
            @Override
            public People next() {
                return peopleList.get(point++);
            }
        }
    }
    
    • 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
  • 相关阅读:
    【机器学习12】集成学习
    Nodejs -- 前后端身份认证概念及在Express中使用认证(Session,Cookie,JWT)
    【分布式架构】单体优化遇瓶颈,负载均衡显神通
    js检测数据类型总结
    web | http 的一些问题 | get/post的区别 | http版本 | http与https的区别 | session、cookie、token
    Ubuntu server 24 (Linux) 普通用户不能sudo 也不能使用root登录 忘记root密码 修复解决方案
    隐私计算迎来千亿级风口,一文讲清它的技术理论基础
    CentOS 7 下将 MySQL 5.6 升级为MySQL 5.7
    解决ul元素不能跟div同一行显示的办法
    为什么你的项目总延期?多半是没做好5件事
  • 原文地址:https://blog.csdn.net/qq_43630812/article/details/126076576