** Table of Contents ** There is no way to express it other than what is written for the purpose, but to put it in more detail, it is like abstracting the parts that can be shared by the algorithm and implementing the behavior in a concrete class.
Define a set of algorithms, encapsulate each algorithm and make them interchangeable. The Strategy pattern allows the algorithm to be modified independently of the clients that use it.
・ Strategy Abstract class of common part ・ ConcreateStrategy Concrete class of common part ・ Context algorithm part
Let's implement bubble sort and shaker sort, which are algorithms for sorting arrays.
Sort.kt
package strategy
interface Sort {
fun swap(index: Int)
fun outOfOrder(index: Int): Boolean
fun length(): Int
fun setArray(array: Any)
}
For Int type arrays.
IntSort.kt
package strategy
class IntSort: Sort {
private lateinit var array: Array<Int>
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]
}
override fun length(): Int {
return array.size
}
override fun setArray(array: Any) {
this.array = array as Array<Int>
}
}
Implement from bubble sort.
BubbleSort.kt
package strategy
class BubbleSort(private val sort: Sort) {
private var operations = 0
private var length = 0
fun sort(array: Any): Int {
sort.setArray(array)
length = sort.length()
operations = 0
if (length <= 1) {
return operations
}
for (nextToLast in length - 2 downTo 0) {
for (index in 0..nextToLast) {
if (sort.outOfOrder(index)) {
sort.swap(index)
}
operations++
}
}
return operations
}
}
Next is shaker sort
ShakerSort.kt
package strategy
class ShakerSort(private val sort: Sort) {
fun sort(array: Any): Int {
sort.setArray(array)
var length = sort.length()
var operations = 0
if (length <= 1) {
return operations
}
var thisPassInOrder = false
loop@ for (nextToLast in length - 2 downTo 0) {
if (thisPassInOrder) {
break@loop
}
thisPassInOrder = true
for (index in 0..nextToLast) {
if (sort.outOfOrder(index)) {
sort.swap(index)
thisPassInOrder = false
}
operations++
}
}
return operations
}
}
Let's sort it now.
Client.kt
package strategy
class Client {
init {
val array1 = arrayOf(22,533,43212,1,567,7,22,2,35,9913)
val array2 = arrayOf(25,77,834534,32,11,3,880)
val intSort = IntSort()
//Bubble sort
BubbleSort(intSort).sort(array1)
println("Bubble sort result")
array1.forEach {
println("$it")
}
//Shaker sort
ShakerSort(intSort).sort(array2)
println("Shaker sort result")
array2.forEach {
println("$it")
}
}
}
[out-put]
Bubble sort result
1
2
7
22
22
35
533
567
9913
43212
Shaker sort result
3
11
25
32
77
880
834534
It is a pattern that enables flexible implementation by code that conforms to DIP.
Create not only an Int type array, but also a `DoubleSort``` that implements the ``` Sort interface
`, for example.
I just change the type of the array set by the setArray method ...
DoubleSort.kt
package strategy
class DoubleSort: Sort {
private lateinit var array: Array<Double>
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]
}
override fun length(): Int {
return array.size
}
override fun setArray(array: Any) {
this.array = array as Array<Double>
}
}
Client.kt
package strategy
class Client {
init {
val array1 = arrayOf(33.5, 5.4, 5.0, 225.4, 225.3)
val array2 = arrayOf(10.4, 10.2, 10.5, 10.1, 10.3, 10.6)
val doubleSort = DoubleSort()
//Bubble sort
BubbleSort(doubleSort).sort(array1)
println("Bubble sort result")
array1.forEach {
println("$it")
}
//Shaker sort
ShakerSort(doubleSort).sort(array2)
println("Shaker sort result")
array2.forEach {
println("$it")
}
}
}
[out-put]
Bubble sort result
5.0
5.4
33.5
225.3
225.4
Shaker sort result
10.1
10.2
10.3
10.4
10.5
10.6
Factory Method It looks like a pattern that goes very well with the pattern.
intsort
Ordoublesort
If the factory method class generates the class, it seems that a very convenient module can be created.
that's all
Recommended Posts