aiyoudiao aiyoudiao
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档

码二

扫微信二维码,认识一下码二吧😉。
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档
  • LeetCode

  • 算法

  • 数据结构

  • 设计模式

    • 浅聊设计理念
    • 谈一谈设计原则
    • 工厂方法模式
    • 抽象工厂模式
    • 建造者模式
    • 原型模式
    • 单例模式
    • 桥接模式
    • 代理模式
    • 装饰器模式
    • 适配器模式
    • 享元模式
    • 组合模式
    • 外观模式
    • 观察者模式
    • 模板方法模式
    • 策略模式
    • 职责链模式
    • 状态模式
    • 迭代器模式
      • 前言
      • 迭代器模式
    • 访问者模式
    • 备忘录模式
    • 命令模式
    • 解释器模式
    • 中介者模式
  • Other

  • vue3设计与实现

  • 算法与设计
  • 设计模式
aiyoudiao
2022-02-24

迭代器模式

# 前言

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

# 迭代器模式

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

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

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}`)
    }

}
#设计模式
上次更新时间: 10年18月2023日 01时57分53秒
状态模式
访问者模式

← 状态模式 访问者模式 →

最近更新
01
01.数据结构导论一览.md
10-16
02
30.2023年06月04日.md
06-04
03
08.与测量相关.md
05-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 aiyoudiao 码二 备案号: 鄂ICP备2022002654号-1