美文网首页
Iterator模式

Iterator模式

作者: 涅槃快乐是金 | 来源:发表于2020-03-28 16:51 被阅读0次

将(Book)放置到书架(BookShelf)中,并将书名按顺序显示

接口:

import Book from "./Book";
//Aggregate接口是所要遍历的集合的接口
export interface Aggregate {
    bookIterator: BookIterator;
}
//Iterator接口,用于遍历集合中的元素
export interface BookIterator {
    hasNext(): boolean;//是否有下一个
    next(): Book;//获取下一个元素
}

Book类:

/**
 * class Book
 */
export default class Book {
    private name: string;
    /**
     * Book
     */
    constructor(name: string) {
        this.name = name;
    }
    public getName(): string {
        return this.name
    }
}

BookShelf类:

import Book from "./Book";
import { BookIterator } from "./Aggregate";
import BookShelfIterator from "./BookShelfIterator";
export default class BookShelf {
    private books: Array<Book>;
    private last: number = 0;
    constructor(maxsize: number) {
        this.books = new Array<Book>(maxsize);
    }
    /**
     * appendBook
     */
    public appendBook(book: Book): void {
        this.books[this.last] = book;
        this.last++;
    }
    /**
     * getBookAt
     */
    public getBookAt(index: number): Book {
        return this.books[index]
    }
    /**
     * getLength
     */
    public getLength(): number {
        return this.last;
    }

    /**
     * name
     */
    public bookIterator(): BookIterator {
        return new BookShelfIterator(this);
    }
}

BookShelfIterator类

 import BookShelf from "./BookShelf";
import Book from "./Book";
import { BookIterator } from "./Aggregate";
export default class BookShelfIterator implements BookIterator {
    private bookShelf: BookShelf;
    private index: number;
    constructor(bookShelf: BookShelf) {
        this.bookShelf = bookShelf;
        this.index = 0;
    }

    /**
     * hasNext
     */
    public hasNext(): boolean {
        if (this.index < this.bookShelf.getLength()) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * next
     */
    public next(): Book {
        const book = this.bookShelf.getBookAt(this.index);
        this.index++;
        return book;
    }
}

index入口

import BookShelf from "./BookShelf";
import Book from "./Book";

let bookShelf = new BookShelf(4);
bookShelf.appendBook(new Book("test1"));
bookShelf.appendBook(new Book("test2"));
bookShelf.appendBook(new Book("test3"));
bookShelf.appendBook(new Book("test4"));
const it = bookShelf.bookIterator();
while (it.hasNext()) {
    const book = it.next();
    console.log(book.getName());
}

相关文章

  • Iterator模式

    迭代器模式(Iterator模式) 定义: 迭代器(Iterator)模式,又叫做游标(Cursor)模式。 ...

  • 设计模式用例(三)

    Strategy 模式 Bridge 模式 Iterator 模式

  • 编程思想

    图解设计模式 Iterator 模式 设计模式 Iterator 模式如何提升你的阅读能力? 导读:今天这篇文章是...

  • 函数式编程下的Iterator模式

    在模式下,Iterator模式是一个思路相对简单的模式。迭代器(Iterator)模式,又叫做游标(Cursor)...

  • 迭代器模式 Iterator Pattern

    迭代器模式(Iterator Pattern)属于行为型模式。Iterator pattern 提供了循环集合的标...

  • 1. Iterator模式-一个一个遍历

    时间: 2019-04-29 1.1 Iterator模式概念 Iterator模式: 用于在数据集合中按照顺序遍...

  • Design(一)-Iterator设计模式

    Iterator设计模式 简介: Iterator设计模式在数据集合中按照顺序遍历集合. 英语单词Iterate有...

  • 图解设计模式Iterator模式

    Iterator设计模式UML图 Iterator(迭代器) ConcreteIterator(具体的迭代器) A...

  • iterator模式

    类似于stl里面的迭代器 iterator.h #ifndef _ITERATOR_H#define _ITERA...

  • Iterator模式

    Iterator模式用于在数据集合中按照顺序遍历集合。英语单词Iterate有重复做某件事情的意思。也叫做“迭代器...

网友评论

      本文标题:Iterator模式

      本文链接:https://www.haomeiwen.com/subject/jnksuhtx.html