4 May 2019, 13:49

the iterator pattern


Name: Iterator

Type: Behavioural

Use when you want to:

Access the elements of an object without exposing the underlying presentation.

From Wikipedia:

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

Real world example:

An old radio set will be a good example of iterator, where user could start at some channel and then use next or previous buttons to go through the respective channels. Or take an example of MP3 player or a TV set where you could press the next and previous buttons to go through the consecutive channels or in other words they all provide an interface to iterate through the respective channels, songs or radio stations.

Typescript Code Example:

Create a radio station:

class RadioStation {

    constructor(private frequency: number) {
        this.frequency = frequency    
    }
    
    getFrequency() {
        return this.frequency
    }
} 

And a list of stations

class StationList {
    
    constructor(public stations: RadioStation[] = []){
    }

    addStation(station) {
        this.stations.push(station)
    }
    
    removeStation(toRemove) {
        const toRemoveFrequency = toRemove.getFrequency()
        this.stations = this.stations.filter(station => {
            return station.getFrequency() !== toRemoveFrequency
        })
    }
}

Iterate through th list:

const stationList = new StationList()

stationList.addStation(new RadioStation(89))
stationList.addStation(new RadioStation(101))
stationList.addStation(new RadioStation(102))
stationList.addStation(new RadioStation(103.2))

stationList.stations.forEach(station => console.log(station.getFrequency()))

stationList.removeStation(new RadioStation(89))
```

← The Mediator Pattern
The Command Pattern →