• 23 行为型模式-迭代器模式


    1 迭代器模式介绍

    迭代器模式是我们学习一个设计时很少用到的、但编码实现时却经常使用到的行为型设计模式。在绝大多数编程语言中,迭代器已经成为一个基础的类库,直接用来遍历集合对象。在平时开发中,我们更多的是直接使用它,很少会从零去实现一个迭代器。
    迭代器模式(Iterator pattern)又叫游标(Cursor)模式,它的原始定义是:迭代器提供一种对容器对象中的各个元素进行访问的方法,而又不需要暴露该对象的内部细节。
    在这里插入图片描述
    在软件系统中,容器对象拥有两个职责: 一是存储数据,而是遍历数据.从依赖性上看,前者是聚合对象的基本职责.而后者是可变化的,又是可分离的.因此可以将遍历数据的行为从容器中抽取出来,封装到迭代器对象中,由迭代器来提供遍历数据
    的行为,这将简化聚合对象的设计,更加符合单一职责原则。

    2 迭代器模式原理

    迭代器模式结构图
    在这里插入图片描述
    迭代器模式主要包含以下角色:
    在这里插入图片描述

    3 迭代器模式实现
    /**
     * 迭代器接口
     **/
    public interface Iterator<E> {
    
        //判断集合中是否有下一个元素
        boolean hasNext();
    
        //将有游标后移一位
        void next();
    
        //返回当前游标指定的元素
        E currentItem();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    /**
     * 具体的迭代器
     **/
    public class ConcreteIterator<E> implements Iterator<E> {
    
        private int cursor; //游标
    
        private ArrayList<E> arrayList;  //容器
    
        public ConcreteIterator(ArrayList<E> arrayList) {
            this.cursor = 0;
            this.arrayList = arrayList;
        }
    
        @Override
        public boolean hasNext() {
            return cursor != arrayList.size();
        }
    
        @Override
        public void next() {
            cursor++;
        }
    
        @Override
        public E currentItem() {
            if(cursor >= arrayList.size()){
                throw new NoSuchElementException();
            }
            return arrayList.get(cursor);
        }
    }
    
    • 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
    public class Test01 {
    
        public static void main(String[] args) {
    
            ArrayList<String> names = new ArrayList<>();
            names.add("lisi");
            names.add("zhangsan");
            names.add("wangwu");
    
    //        Iterator iterator = new ConcreteIterator<>(names);
    //        while(iterator.hasNext()){
    //            System.out.println(iterator.currentItem());
    //            iterator.next();
    //        }
    
            java.util.Iterator<String> iterator1 = names.iterator();
            while(iterator1.hasNext()){
                System.out.println(iterator1.next());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    4 迭代器模式应用实例
    /**
     * 抽象迭代器
     **/
    public interface IteratorIterator<E> {
    
        void reset();  //重置为第一个元素
    
        E next(); //获取下一个元素
    
        E currentItem();  //检索当前元素
    
        boolean hasNext(); //判断集合中是否还有下一个元素
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    /**
     * 抽象集合
     **/
    public interface ListList<E> {
    
        //获取迭代器对象的抽象方法
        IteratorIterator<E> iterator();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    /**
     * 主题类
     **/
    public class Topic {
    
        private String name;
    
        public Topic(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    /**
     * 具体迭代器
     **/
    public class TopicIterator implements IteratorIterator<Topic> {
    
        //Topic数组
        private Topic[] topics;
    
        //记录存储位置的有游标
        private int position;
    
        public TopicIterator(Topic[] topics) {
            this.topics = topics;
            position = 0;
        }
    
        @Override
        public void reset() {
            position = 0;
        }
    
        @Override
        public Topic next() {
            return topics[position++];
        }
    
        @Override
        public Topic currentItem() {
            return topics[position];
        }
    
        @Override
        public boolean hasNext() {
            if(position >= topics.length){
                return false;
            }
            return true;
        }
    }
    
    • 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
    /**
     * 具体集合类
     **/
    public class TopicList implements ListList<Topic>{
    
        private Topic[] topics;
    
        public TopicList(Topic[] topics) {
            this.topics = topics;
        }
    
        @Override
        public IteratorIterator<Topic> iterator() {
            return new TopicIterator(topics);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    public class Client {
    
        public static void main(String[] args) {
            Topic[] topics = new Topic[4];
            topics[0] = new Topic("t1");
            topics[1] = new Topic("t2");
            topics[2] = new Topic("t3");
            topics[3] = new Topic("t4");
    
            TopicList topicList = new TopicList(topics);
            IteratorIterator<Topic> iterator = topicList.iterator();
    
            while(iterator.hasNext()){
                Topic topic = iterator.next();
                System.out.println(topic.getName());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    5 迭代器模式总结

    在这里插入图片描述
    3) 使用场景
    在这里插入图片描述

  • 相关阅读:
    性能优于BERT的FLAIR:一篇文章入门Flair模型
    Operations for Algorithms
    Serverless Streaming:毫秒级流式大文件处理探秘
    4. Java 的线程安全机制之`volatile`
    es带用户名密码验证并配置elasticsearch-head连接
    【送书活动】强势挑战Java,Kotlin杀回TIOBE榜单Top 20!学Kotlin看哪些书?
    六、决策树算法(DT,DecisionTreeClassifier)(有监督学习)
    Linux服务器挖矿病毒处理
    【UCIe】UCIe Data to Clock
    一种用于干式脑电图的高密度256通道电极帽
  • 原文地址:https://blog.csdn.net/weixin_39563769/article/details/134084012