Iterator Design Pattern in Kotlin

1. Definition

The Iterator Design Pattern provides a way to access elements of a collection object sequentially without exposing its underlying representation. It decouples the collection from the iteration logic.

2. Problem Statement

Suppose you have a collection of items, like a bookshelf with various books. You want to provide a way to iterate through these books without exposing the internal structure of the bookshelf. How can you achieve this without making the bookshelf's implementation details public?

3. Solution

The Iterator Pattern solves this by introducing separate objects (Iterators) that encapsulate the iteration logic. This way, the collection remains untouched, and the iteration logic is abstracted away.

4. Real-World Use Cases

1. Navigating through a playlist of songs.

2. Iterating through a collection of social media posts.

3. Traversing different types of tree structures.

5. Implementation Steps

1. Define an Iterator interface.

2. Implement the Iterator for the specific collection.

3. Use the Iterator to access and traverse the collection.

6. Implementation in Kotlin

// Step 1: Define the Iterator interface
interface Iterator<T> {
    fun hasNext(): Boolean
    fun next(): T
// Step 2: Implement the Iterator for a specific collection
class Book(val name: String)
class BookShelf : Iterable<Book> {
    private val books = mutableListOf<Book>()
    fun add(book: Book) {
    override fun iterator(): Iterator<Book> {
        return BookShelfIterator(books)
    class BookShelfIterator(private val books: List<Book>) : Iterator<Book> {
        private var index = 0
        override fun hasNext(): Boolean {
            return index < books.size
        override fun next(): Book {
            return books[index++]
// Step 3: Use the Iterator to access and traverse the collection
fun main() {
    val bookShelf = BookShelf()
    bookShelf.add(Book("Book A"))
    bookShelf.add(Book("Book B"))
    bookShelf.add(Book("Book C"))
    for (book in bookShelf) {


Book A
Book B
Book C


1. We start by defining a generic Iterator interface that provides methods for checking if there's a next element (hasNext()) and for retrieving the next element (next()).

2. We implement the collection (BookShelf) that uses a list of Book objects internally. The BookShelf provides an iterator through its iterator() method, which returns an instance of BookShelfIterator.

3. The BookShelfIterator implements our Iterator interface and contains the actual logic for traversing the collection.

4. In the main function, we can now iterate over the BookShelf using a standard for-loop. The underlying details of how the books are stored or how the iteration is performed are hidden, making our code cleaner and more maintainable.

7. When to use?

Use the Iterator Pattern when:

1. You want to provide a standard way to iterate over a collection without exposing its internal structure.

2. You have different ways of iterating over a collection and you want to provide multiple iterators.

3. You want to decouple your collection from specific algorithms used for traversal or iteration.