Template Method | Cheat Sheet

Behavioral Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
3 min readJul 19, 2021

--

Template Method is known as one of the cleanest inheritance techniques. The pattern is defined by setting the basic structure of an algorithm in an operation and delegating flow steps to subclasses. This allows the subclasses to override certain steps of an algorithm without changing its structure. This leads to high reusability, maintainability, and readability.

Real-life examples

  • Different order types (online shop, in a store) that need a template for the flow of an order process
  • Web pages that process similar things and thus differ minimally
  • Template for house construction (different options for an entrance, roof type, heating type, …)

Meaning

  • Definition of the basic structure of an algorithm in an operation as well as delegation of some process steps to subclasses
  • Allows subclasses to override certain steps of an algorithm without changing its structure

Applicability (suitable if …)

  • An immutable part of an algorithm is supposed to be implemented once and it is left to subclasses to implement behavior classes that are mutable
  • Common behaviors are to be outsourced to subclasses (refactoring to generalize)
  • The extension of subclasses is supposed to be regulated (hook)

Assets and Drawbacks

  • Basic technique of reuse
  • One of the cleanest inheritances
  • Has special value in class libraries
  • Also called Hollywood Principle (“Don’t call us, we’ll call you.”): base class calls operation of subclass

Example

Every human being has his or her own morning routine, which in certain respects runs in the same way. You get up, eat breakfast, exercise, go to work, and so on. From this, you can easily create a Template that is executed differently by different people (subclasses).

abstract base class

A person who is lazy about sport is more likely to reach for a book in the morning:

subclass 1

A sportsman likes to go running in the morning instead of reading a book:

subclass 2

The client can create various morning reports for each kind of person very easily with this implementation. Also, it is well readable, maintainable, and easily expandable by other programmers.

client

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Behavioral Patterns deal with algorithms and assignment of responsibilities to objects. Furthermore, they describe mutual communication patterns and grasp complex program sequences. They are therefore very well suited for use in areas where complicated code needs to be made readable and maintainable. Extensions are in consequence very good and quickly implementable.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links