In this article, we will learn how to use and implement the Iterator Pattern in TypeScript with an example.
Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
TypeScript Iterator Pattern Example
Let's create iterator.ts file and add the following code to it:
export interface Iterator {
next(): any;
hasNext(): boolean;
}
export interface Aggregator {
createIterator(): Iterator;
}
export class ConcreteIterator implements Iterator {
private collection: any[] = [];
private position: number = 0;
constructor(collection: any[]) {
this.collection = collection;
}
public next(): any {
// Error handling is left out
var result = this.collection[this.position];
this.position += 1;
return result;
}
public hasNext(): boolean {
return this.position < this.collection.length;
}
}
export class Numbers implements Aggregator {
private collection: number[] = [];
constructor(collection: number[]) {
this.collection = collection;
}
public createIterator(): Iterator {
return new ConcreteIterator(this.collection);
}
}
Usage
Let's create demo.ts file and add the following code to it:
import { ConcreteIterator, Numbers } from "./iterator";
export function show() : void {
var nArray = [1, 7, 21, 657, 3, 2, 765, 13, 65],
numbers: Numbers = new Numbers(nArray),
it: ConcreteIterator = <ConcreteIterator>numbers.createIterator();
while (it.hasNext()) {
console.log(it.next());
}
}
show();
Run:
- Compile the above code using the TypeScript compiler.
- Above code is compiled to plan JavaScript code
- Run Javascript code using node
design_patterns_in_typescript\iterator> tsc --target ES5 .\demo.ts
design_patterns_in_typescript\iterator> node .\demo.js
1
7
21
657
3
2
765
13
65
All TypeScript Design Patterns
1. Creational Design Patterns
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
- TypeScript Singleton Pattern Example
- TypeScript Factory Design Pattern with Example
- TypeScript Abstract Factory Pattern Example
- TypeScript Builder Pattern Example
- TypeScript Prototype Pattern Example
2. Structural Design Patterns
Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
- TypeScript Bridge Pattern Example
- TypeScript Adapter Pattern Example
- TypeScript Decorator Pattern Example
- TypeScript Composite Pattern Example
- TypeScript Flyweight Design Pattern Example
- TypeScript Facade Pattern Example
- TypeScript Proxy Pattern Example
3. Behavioral Design Patterns
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
- TypeScript Command Pattern Example
- TypeScript Chain of Responsibility Pattern Example
- TypeScript Visitor Pattern Example
- TypeScript Template Method Pattern Example
- TypeScript Strategy Pattern Example
- TypeScript State Pattern Example
- TypeScript Observer Pattern Example
- TypeScript Memento Pattern Example
- TypeScript Mediator Pattern Example
- TypeScript Iterator Pattern Example
- TypeScript Interpreter Design Pattern Example