[JAVA] [Gang of Four] Design pattern learning --Template Method

Template Method-Template

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.

Purpose

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.

Component

・ AbstractClass common part ・ ConcreteClass Not common

Implementation

Strategy Implement a program that sorts arrays as well as patterns.

AbstractClass common part

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
}

ConcreteClass parts that are not common

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 ...

People who use

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)
        }
    }
}

Output result

[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

[Gang of Four] Design pattern learning --Template Method
[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning
[Gang of Four] Design pattern learning --Singleton
[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 --Iterator
[Gang of Four] Design pattern learning --Facade
[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 --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 --Chain of Responsibility
Design Pattern #Template Method
Python Design Pattern --Template method
Learn the design pattern "Template Method" in Python
[Details] Template Method pattern
Template Method pattern in Java
Introduction of data-driven controller design method
Gang of Four (GoF) Patterns in Python
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Learn the design pattern "Factory Method" in Python
Clustering of clustering method
Design Pattern #Builder
Design Pattern #Adapter
Design Pattern #Decorator
Design Pattern #Observer
Design Pattern #Facade
Design Pattern #Strategy
Design Pattern #Singleton
Design Pattern #Proxy
PyTorch learning template
Learn the design pattern "Chain of Responsibility" in Python
I wrote a design pattern in kotlin Template edition