Iterator
- 컬렉션 요소들의 기본 표현(리스트, 스택, 트리 등)을 노출하지 않고 하나씩 순회할 수 있도록 함
- 컬렉션의 순회 동작을 반복자라는 별도의 객체로 추출
- 컬렉션이 내부에 복잡한 데이터 구조가 있지만 구조의 복잡성을 보안이나 편의상의 이유로
클라이언트들로부터 숨기고 싶을 때 사용
- 앱 전체에서 순회 코드의 중복을 줄이기
- 코드가 다른 데이터 구조들을 순회할 수 있기 원할 때, 또는 이러한 구조들의 유형을 미리 알 수 없을 때 사용
- 복잡한 데이터 구조의 내부 세부 정보를 노출하지 않고 해당 구조를 차례대로 순회할 수 있도록 함
- 예시
/**
* Iterator Design Pattern
*
* Intent: Lets you traverse elements of a collection without exposing its
* underlying representation (list, stack, tree, etc.).
*/
interface Iterator<T> {
// Return the current element.
current(): T;
// Return the current element and move forward to next element.
next(): T;
// Return the key of the current element.
key(): number;
// Checks if current position is valid.
valid(): boolean;
// Rewind the Iterator to the first element.
rewind(): void;
}
interface Aggregator {
// Retrieve an external iterator.
getIterator(): Iterator<string>;
}
/**
* Concrete Iterators implement various traversal algorithms. These classes
* store the current traversal position at all times.
*/
class AlphabeticalOrderIterator implements Iterator<string> {
private collection: WordsCollection;
/**
* Stores the current traversal position. An iterator may have a lot of
* other fields for storing iteration state, especially when it is supposed
* to work with a particular kind of collection.
*/
private position: number = 0;
/**
* This variable indicates the traversal direction.
*/
private reverse: boolean = false;
constructor(collection: WordsCollection, reverse: boolean = false) {
this.collection = collection;
this.reverse = reverse;
if (reverse) {
this.position = collection.getCount() - 1;
}
}
public rewind() {
this.position = this.reverse ? this.collection.getCount() - 1 : 0;
}
public current(): string {
return this.collection.getItems()[this.position];
}
public key(): number {
return this.position;
}
public next(): any {
const item = this.collection.getItems()[this.position];
this.position += this.reverse ? -1 : 1;
return item;
}
public valid(): boolean {
if (this.reverse) {
return this.position >= 0;
}
return this.position < this.collection.getCount();
}
}
/**
* Concrete Collections provide one or several methods for retrieving fresh
* iterator instances, compatible with the collection class.
*/
class WordsCollection implements Aggregator {
private items: string[] = [];
public getItems(): string[] {
return this.items;
}
public getCount(): number {
return this.items.length;
}
public addItem(item: string): void {
this.items.push(item);
}
public getIterator(): Iterator<string> {
return new AlphabeticalOrderIterator(this);
}
public getReverseIterator(): Iterator<string> {
return new AlphabeticalOrderIterator(this, true);
}
}
/**
* The client code may or may not know about the Concrete Iterator or Collection
* classes, depending on the level of indirection you want to keep in your
* program.
*/
const collection = new WordsCollection();
collection.addItem('First');
collection.addItem('Second');
collection.addItem('Third');
const iterator = collection.getIterator();
console.log('Straight traversal:');
while (iterator.valid()) {
console.log(iterator.next());
}
console.log('');
console.log('Reverse traversal:');
const reverseIterator = collection.getReverseIterator();
while (reverseIterator.valid()) {
console.log(reverseIterator.next());
}
- 활용
반응형
'Personal-Study > Design Patterns' 카테고리의 다른 글
[TS Design Patterns] 행동 패턴 - 옵저버 (2) | 2023.11.12 |
---|---|
[TS Design Patterns] 행동 패턴 - 메멘토 (0) | 2023.11.07 |
[TS Design Patterns] 행동 패턴 - 커맨드 (0) | 2023.11.05 |
[TS Design Patterns] 행동 패턴 - 중재자 (0) | 2023.11.04 |
[TS Design Patterns] 행동 패턴 - 책임 연쇄 (2) | 2023.11.01 |
댓글