Table of Contents It is a pattern that abstracts the common part of the algorithm and implements the part with different behavior in a concrete class. If you read only the explanation, it is the same as the Strategy pattern, but it is realized by delegation over there and inheritance over here.
Inheritance makes the dependency stronger, so personally, I think the Strategy pattern is better. I think.
However, this one requires fewer classes to implement.
We will define the skeleton of the algorithm for one operation and leave some steps in it to the definition in the subclass. The Template Method pattern redefines a step in an algorithm with a subclass without changing the structure of the algorithm.
・ AbstractClass common part ・ ConcreteClass Not common
Strategy Implement a program that sorts arrays as well as patterns.
BubbleSort.kt
package templatemethod
abstract class BubbleSort {
private var operations = 0
protected var length = 0
protected fun doSort(): Int {
operations = 0
if (length <= 1) return operations
for (nextToLast in length - 1 downTo 0) {
for (index in 0 until nextToLast) {
if (outOfOrder(index)) swap(index)
operations++
}
}
return operations
}
protected abstract fun swap(index: Int)
protected abstract fun outOfOrder(index: Int): Boolean
}
Int type array concrete class
IntBubbleSort.kt
package templatemethod
class IntBubbleSort: BubbleSort() {
private lateinit var array: Array<Int>
fun sort(theArray: Array<Int>): Int {
array = theArray
length = theArray.size
return doSort()
}
override fun swap(index: Int) {
val temp = array[index]
array[index] = array[index + 1]
array[index + 1] = temp
}
override fun outOfOrder(index: Int): Boolean {
return array[index] > array[index + 1]
}
}
Double type array concrete class
DoubleBubbleSort.kt
package templatemethod
class DoubleBubbleSort: BubbleSort() {
private lateinit var array: Array<Double>
fun sort(theArray: Array<Double>): Int {
array = theArray
length = theArray.size
return doSort()
}
override fun swap(index: Int) {
val temp = array[index]
array[index] = array[index + 1]
array[index + 1] = temp
}
override fun outOfOrder(index: Int): Boolean {
return array[index] > array[index + 1]
}
}
Well, this can also be achieved in one class using Generics ...
Client.kt
package templatemethod
class Client {
init {
val intArray = arrayOf(332, 1, 13, 3232, 456, 22, 5)
println("Before sorting Int array")
intArray.forEach {
println(it)
}
//Sorting
IntBubbleSort().sort(intArray)
println("After sorting Int array")
intArray.forEach {
println(it)
}
val doubleArray = arrayOf(10.01, 10.5, 10.4123, 10.12, 10.87)
println("Before double array sorting")
doubleArray.forEach {
println(it)
}
//Sorting
DoubleBubbleSort().sort(doubleArray)
println("After double array sorting")
doubleArray.forEach {
println(it)
}
}
}
[out-put]
Before sorting Int array
332
1
13
3232
456
22
5
After sorting Int array
1
5
13
22
332
456
3232
Before double array sorting
10.01
10.5
10.4123
10.12
10.87
After double array sorting
10.01
10.12
10.4123
10.5
10.87
It's a pretty basic pattern. I think it will be used everywhere.
that's all
Recommended Posts