我学会了,迭代器模式

简介: 迭代器模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

前言

迭代器模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

迭代器模式

使用场景:不直接暴露一个对象的内部显示,通过返回的一个迭代器来实现遍历的效果。让存储数据的容器和获取容器内部数据的行为解耦。

理解:这是一种类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,很特别。将数据的遍历操作和数据集合解耦了,当然它本来就是用来遍历集合对象,非常的方便。

namespace action_mode_06 {

    // 接口
    interface Iterator {

        hasNext(): boolean

        next(): Object

    }

    // 用于遍历学生列表的迭代器
    class StudentIterator implements Iterator {

        private studentList: StudentList
        private cur: number = 0;

        constructor(studentList: StudentList) {
            this.studentList = studentList
        }

        hasNext(): boolean {
            const result = this.studentList.list.length > this.cur && this.cur > -1;

            if (this.cur === this.studentList.list.length) {
                this.cur = 0;
            }

            return result
        }
        next(): Student {

            if (this.cur === this.studentList.list.length) {
                this.cur = 0
            }

            const result = this.studentList.list[this.cur]
            this.cur++

            return result
        }
    }

    // 学生模型
    class Student {

        name: string;
        classLevel: number;
        sex: string;

        constructor(name: string, classLevel: number, sex: string) {
            this.name = name;
            this.classLevel = classLevel;
            this.sex = sex;
        }

        getIterator() {

        }
    }

    // 学生列表
    class StudentList {

        list: Array<Student> = new Array()

        push(student: Student) {
            this.list.push(student)
            return this.list.length
        }

        getIterator(): StudentIterator {
            return new StudentIterator(this)
        }
    }

    // 使用
    const studentList = new StudentList()
    for (let i = 0; i < 30; i++) {
        const student = new Student(`zs${i}`, i, i % 2 === 0 ? '男' : '女')
        studentList.push(student)
    }

    const iterator = studentList.getIterator()
    
    // 遍历第一次 
    while(iterator.hasNext()) {
        const student = iterator.next()
        console.log(`${student.name}:我来自${student.classLevel}班,性别:${student.sex}`)
    }

    // 遍历第二次
    while(iterator.hasNext()) {
        const student = iterator.next()
        console.log(`${student.name}:我来自${student.classLevel}班,性别:${student.sex}`)
    }

}
目录
相关文章
|
3月前
|
设计模式 Java 数据挖掘
聊聊Java设计模式-迭代器模式
迭代器(Iterator)模式,也叫做游标(Cursor)模式。我们知道,在Java 容器中,为了提高容器遍历的方便性,我们利用迭代器把遍历逻辑从不同类型的集合类中抽取出来,从而避免向外部暴露集合容器的内部结构。
44 0
聊聊Java设计模式-迭代器模式
|
4月前
|
设计模式 uml C++
行为型 迭代器模式
行为型 迭代器模式
20 0
|
6月前
|
设计模式 存储 算法
设计模式~迭代器模式(Iterator)-20
迭代器模式(Iterator Pattern)是Java和.Net编程环境中非常常用的设计模式。这种模式用于顺序访问集合对象的元素,不需要知道集合对象的底层表示。迭代器模式属于行为型模式。迭代器模式已经被淘汰,java中已经把迭代器运用到各个聚集类(collection)中了,使用java自带的迭代器就已经满足我们的需求了 目录 迭代器模式(Iterator) (1)优点 (2)缺点 (3)使用场景 (4)注意事项 (5)应用实例: 代码
25 0
|
9月前
|
设计模式 存储 Java
迭代器模式
迭代器模式(Iterator Pattern)是一种行为型设计模式,它提供了一种顺序访问聚合对象中每个元素的方法,而不暴露其内部实现。
57 1
|
10月前
|
容器
关于迭代器模式我所知道的
关于迭代器模式我所知道的
40 0
|
11月前
|
设计模式 Java 索引
Java设计模式-迭代器模式(Iterator)
Java设计模式-迭代器模式(Iterator)
|
JavaScript 前端开发
简单理解迭代器模式
这几天研究了一下迭代器模式,期间有一段时间经常搞不太懂一些概念与概念之间的关系,今天来整理一下。
102 0
|
设计模式 算法 Java
Java设计模式 ->迭代器模式
Java设计模式 ->迭代器模式
64 0
|
Java 容器
迭代器模式
迭代器模式
82 0
|
Java uml 容器
被用到炉火纯清的迭代器模式
迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示。
95 0
被用到炉火纯清的迭代器模式