DEV Community

Oluwasanmi Aderibigbe
Oluwasanmi Aderibigbe

Posted on

Head First Design Pattern: 7 of 10

I just learnt a new design from the Head First Design Pattern book. Today, I learnt about the Template pattern.

According Head First Design Patterns, The template pattern is a pattern used to define the skeleton of an algorithm in a method(Template method), deferring some steps to subclasses. Template pattern lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

For example you were asked to develop an app for making different kinds of burgers. Making burgers for the most part require the same steps. You could create a class for each burger you'd like to create or you could reuse code by implementing the template pattern.

Implementing the Template pattern is pretty straightforward.

1 First you have define the algorithm in an abstract class. Methods that must be implemented by the subclasses are marked as abstract.

abstract class Burger {
    abstract fun grillMeat()
    abstract fun addToppings()
    fun makePatties() {
        println("warming buns")
    }

    open fun deliver() {}

    fun prepareBurger() {
        makePatties()
        grillMeat()
        addToppings()
        deliver()
    }
}

Enter fullscreen mode Exit fullscreen mode

2 Subclass the template class and provide the required behaviour

class ChickenBurger : Burger() {
    override fun grillMeat() {
        println("Grill chicken")
    }

    override fun addToppings() {
        println("Add things needed for chicken burger")
    }
}

class HamBurger : Burger() {
    override fun grillMeat() {
        println("Grill chicken")
    }

    override fun addToppings() {
        println("Add things needed for ham burger")
    }
}
Enter fullscreen mode Exit fullscreen mode

With the Template pattern we reused code by moving almost everything required to make a burger into an abstract super class and then allow subclasses provide the missing steps.

The Template pattern can be improved by adding hooks. Hooks are essentially methods with default or empty implementation. They give the subclasses the ability to hook into the algorithm at various points. Hooks are somewhat of callbacks so to speak.

abstract class Burger {
    abstract fun grillMeat()
    abstract fun addToppings()
    fun makePatties() {
        println("warming buns")
    }

    open fun deliver() {}

    fun prepareBurger() {
        makePatties()
        grillMeat()
        addToppings()
        deliver()
    }
}

class ChickenBurger : Burger() {
    override fun grillMeat() {
        println("Grill chicken")
    }

    override fun addToppings() {
        println("Add things needed for chicken burger")
    }

    override fun deliver() {
        println("Deliver chicken burger")
    }
}

class HamBurger : Burger() {
    override fun grillMeat() {
        println("Grill chicken")
    }

    override fun addToppings() {
        println("Add things needed for ham burger")
    }
}
Enter fullscreen mode Exit fullscreen mode

The code above implements a hook method called deliver. Burgers that are deliver-ably would simply override that method.

The template pattern is used a lot when it comes to creating frameworks, where the framework controls how things get done, but leaves you to specify some details.

The template pattern is prevalent in Android app development. An example of the template pattern is subclassing Fragments, Activities, anything that has to do with the Android SDK. Whenever you override any event method e.g onCreate(), you are essential hooking into the Android SDK.

Top comments (0)