Last active
June 20, 2021 10:02
-
-
Save YutoKashiwagi/abbc8932d3f31f6ada50241cd551c24c to your computer and use it in GitHub Desktop.
デザインパターン: Iteratorパターン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IAggregate<T> { | |
iterator: () => T | |
} | |
interface IIterator<T> { | |
hasNext: () => boolean; | |
next: () => T; | |
} | |
class Book { | |
private readonly book: string | |
constructor(book: string) { | |
this.book = book | |
} | |
getName = (): string => { | |
return this.book | |
} | |
} | |
class BookShelf implements IAggregate<BookShelfIterator> { | |
private books: Book[] | |
private last: number | |
constructor() { | |
this.books = [] | |
this.last = 0 | |
} | |
getBookAt = (index: number): Book => { | |
return this.books[index] | |
} | |
appendBook = (book: Book): void => { | |
this.books[this.last] = book | |
this.last++ | |
} | |
getLength = (): number => { | |
return this.last | |
} | |
/** | |
* BookShelfに対応するIteratorインスタンスを返す | |
*/ | |
iterator = (): BookShelfIterator => { | |
return new BookShelfIterator(this) | |
} | |
} | |
class BookShelfIterator implements IIterator<Book> { | |
private bookShelf: BookShelf | |
private index: number | |
constructor(bookShelf: BookShelf) { | |
this.bookShelf = bookShelf | |
this.index = 0 | |
} | |
hasNext = (): boolean => { | |
return this.index < this.bookShelf.getLength() | |
} | |
next = (): Book => { | |
const book = this.bookShelf.getBookAt(this.index) | |
this.index++ | |
return book | |
} | |
} | |
class Main { | |
constructor() { | |
let bookShelf = new BookShelf() | |
bookShelf.appendBook(new Book('book A')) | |
bookShelf.appendBook(new Book('book B')) | |
bookShelf.appendBook(new Book('book C')) | |
bookShelf.appendBook(new Book('book D')) | |
const iterator = bookShelf.iterator() | |
while (iterator.hasNext()) { | |
const book = iterator.next() | |
console.log(book.getName()) | |
} | |
} | |
} | |
new Main() | |
// book A | |
// book B | |
// book C | |
// book D |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment