Template Method Design Pattern in Kotlin

1. Definition

The Template Method Design Pattern defines the structure of an algorithm in a method of an abstract class while allowing subclasses to provide a specific implementation for some steps of the algorithm.

2. Problem Statement

Imagine you're developing a software framework for building applications. While the framework sets the overall application structure, you want to allow developers to customize certain parts of the application without altering the framework itself.

3. Solution

The Template Method pattern provides a solution by defining the algorithm's structure in a base class method (often marked as 'final' to prevent overriding) and deferring some steps to subclasses. This way, developers can extend and customize the specific steps without changing the overall algorithm's structure.

4. Real-World Use Cases

1. Data mining tasks where the overall process is the same, but the data processing might vary.

2. In GUI libraries, custom drawing or event handling mechanisms.

3. Beverage preparation process where steps like boiling and serving are the same, but brewing differs (e.g., tea vs. coffee).

5. Implementation Steps

1. Create an abstract base class with a template method.

2. The template method defines the structure of the algorithm.

3. Delegate variable steps to abstract methods in the base class.

4. Concrete subclasses provide an implementation for these variable steps.

6. Implementation in Kotlin

// Step 1: Abstract base class
abstract class Game {
    // Template method
    fun play() {
    abstract fun initialize()
    abstract fun startPlay()
    fun endPlay() {
        println("Game Finished!")
// Step 2: Concrete Subclasses
class Football : Game() {
    override fun initialize() {
        println("Football Game Initialized!")
    override fun startPlay() {
        println("Football Game Started!")
class Basketball : Game() {
    override fun initialize() {
        println("Basketball Game Initialized!")
    override fun startPlay() {
        println("Basketball Game Started!")
// Testing the implementation
fun main() {
    val footballGame = Football()
    val basketballGame = Basketball()


Football Game Initialized!
Football Game Started!
Game Finished!
Basketball Game Initialized!
Basketball Game Started!
Game Finished!


1. We define a generic Game class that contains the play() template method. This method sets the structure for playing any game.

2. The steps like initialize() and startPlay() are left abstract so that specific games can provide their implementation.

3. The endPlay() method is common for all games and thus implemented in the base class.

4. We have two concrete games: Football and Basketball. They provide their own implementations for the initialize() and startPlay() methods.

5. When we run the program, the play() method ensures that the games follow the defined structure but allows for specific game initialization and start processes.

7. When to use?

Use the Template Method Pattern when:

1. You want to provide a generic structure for an algorithm but allow subclasses to implement specific steps.

2. Common behaviors among subclasses should be factored out to avoid duplication.

3. You want to provide a hook for subclasses to react to specific steps of the algorithm without changing its overall structure.