[JAVA] [Gang of Four] Design pattern learning --Iterator

Iterator-Iterator

** Table of Contents ** If you're using a modern language, it's a pattern you wouldn't bother to implement.

But are you going to follow in the footsteps of our predecessors when there was no such module in the standard library? I will implement it with.

Purpose

Provides a way to access the elements of an aggregate object in sequence without exposing the underlying internal representation.

(If you define List firmly, it will be expressed like this ...)

Component

-Abstract class for accessing Iterator elements in order ・ Concrete class of ConcreteIterator Iterator class -Abstract class for creating an instance of Aggregate Iterator type -Concrete Aggregate Aggregate class concrete class

Implementation

Let's implement something like Java's Iterator. What implementation and when should ** Aggregate ** and ** Concrete Aggregate ** be used? I think that it is unnecessary if Iterator is implemented in the generic type.

Abstract class for sequential access to Iterator elements

Iterator.kt


package iterator

interface Iterator<T> {
    var index: Int
    var list: ArrayList<T>

    /**
     *Get the indexth element
     */
    fun elementAt(index: Int): T

    /**
     *Check if there are still elements left
     */
    fun hasNext(): Boolean

    /**
     *Get the next element
     */
    fun next(): T

    /**
     *Add an element
     */
    fun add(element: T)

    /**
     *Delete the indexth element
     */
    fun remove(index: Int)
}

ConcreteIterator Iterator class concrete class

ConcreteIterator.kt


package iterator

class ConcreteIterator<T>(vararg elements: T): Iterator<T> {

    override var index = 0
    override var list = ArrayList<T>()

    init {
        list.addAll(elements)
    }

    override fun elementAt(index: Int): T {
        return list[index]
    }

    override fun hasNext(): Boolean {
        return index != list.size
    }

    override fun next(): T {
        return list[index++]
    }

    override fun add(element: T) {
        list.add(element)
    }

    override fun remove(index: Int) {
        list.removeAt(index)
    }
}s

People who use

Client.kt


package iterator

class Client {
    init {
        val strIterator = makeStrIterator()
        while(strIterator.hasNext()) {
            println(strIterator.next())
        }

        val intIterator = makeIntIterator()
        while (intIterator.hasNext()) {
            println(intIterator.next())
        }
    }

    private fun makeStrIterator(): Iterator<String> {
        val iterator = ConcreteIterator("Ichi", "To", "Mr.")
        iterator.add("Yon")

        return iterator
    }

    private fun makeIntIterator(): Iterator<Int> {
        return ConcreteIterator(1, 2, 3)
    }
}

Output result

[out-put]
Ichi
To
Mr.
Yon
1
2
3

With the above, I was able to know the hardships of my predecessors.

Recommended Posts

[Gang of Four] Design pattern learning --Iterator
[Gang of Four] Design pattern learning
[Gang of Four] Design Pattern Learning --Decorator
[Gang of Four] Design pattern learning --Visitor
[Gang of Four] Design pattern learning --Mediator
[Gang of Four] Design pattern learning --Facade
[Gang of Four] Design pattern learning --Composite
[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning --Memento
[Gang of Four] Design pattern learning --State
[Gang of Four] Design pattern learning --Interpreter
[Gang of Four] Design pattern learning --Builder
[Gang of Four] Design pattern learning --Bridge
[Gang of Four] Design pattern learning --Proxy
[Gang of Four] Design pattern learning --Strategy
[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning --Observer
[Gang of Four] Design pattern learning --Command
[Gang of Four] Design pattern learning --Fly Weight
[Gang of Four] Design pattern learning --Abstract Factory
[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning --Chain of Responsibility
[Gang of Four] Design pattern learning --Template Method
Gang of Four (GoF) Patterns in Python
Learn the design pattern "Iterator" in Python
Design Pattern #Builder
Design Pattern #Decorator
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Design Pattern #Observer
Design Pattern #Facade
Design Pattern #Strategy
Design Pattern #Singleton
Design Pattern #Proxy
I wrote a design pattern in kotlin, Iterator edition
Learn the design pattern "Chain of Responsibility" in Python
Design Pattern #Factory Method
Iterator pattern in Java
Deep learning 1 Practice of deep learning
Design Pattern #Template Method
GoF design pattern is just an "introduction of abstraction layer"