Design It The Templated Way!

Rohit Kelkar
Globant
Published in
4 min readAug 25, 2022

Hey fellow programmers and enthusiasts!

You might have heard of encapsulating object creation, method invocation, and other complex interfaces during your programming journey. What you might not be aware of is encapsulating algorithms. Let’s try to take a glance at how the template method design pattern helps us in achieving algorithm encapsulation. We will look at what is the pattern about, why it is needed, and where we can use it. Let’s go!

What is the Template method design pattern?

A template method design pattern is a behavioral design pattern that provides an abstract definition of an algorithm with the help of an abstract method called the Template Method. The purpose is to define the skeleton of an algorithm by providing responsibility for some steps to subclasses i.e. the subclasses can define or override certain steps without affecting the skeleton.

Why do we need it and its usage -

This pattern helps in achieving code reusability and removing code redundancy. At times we need to design common libraries and frameworks that could be reused by our systems. In such scenarios, template methods are our way of separating out the common behavior. The applications developed with this design pattern allow the low-level components to fit themselves into high-level components and allow them to have modular control over how they should behave within the system.

The template can be created once and child classes can override the behavior of abstract methods as per the need without disturbing the structure of the algorithm. In this pattern, each child calls either an abstract or a primitive operation. A subclass provides requirement-specific details to the algorithm by defining the abstract operations.

What are the use cases?

  • The cases where we wish to create a framework where the framework classes can define some behavior and shoulder some responsibilities to the user application.
  • The cases where we want to have a template setup so that new users follow the same for consistency.
  • While creating reusable interfaces and UI with small modifications on different screens.

Which Anti-Pattern does it help to resolve?

This pattern helps in resolving the ‘Call super Anti-pattern’. It is an anti-pattern where the child class has to call its parent class method using the super keyword, in order to make a functionality complete. Calling the parent class method using the Super keyword is not a wrong practice in OOP. But if it stands as a necessary condition to complete the business logic then it is known as a code smell or ‘Call super Anti-pattern’, as it does not make its expectation forceful to the programmer.

The template method design pattern helps fix this anti-pattern by reversing this responsibility to the child classes. The abstract methods referenced inside the template method in the parent class force the child class to implement the business logic and hence provides more control to the child class.

Code with an example.

Let’s consider an example of a typical mobile app screen. This includes data loading, control creations, setting frames, and post-render callbacks. A general template can be created as a base class that would run for every screen and performs different tasks. The responsibilities and definition of the methods can differ from screen to screen.

For example, we might need different data loading logic in some classes, different rendering logic, and so on. These responsibilities can be shouldered on the individual screens. But a common template method can be created which calls these methods step by step and decides its behavior as runtime polymorphism.

  1. Base page with Template method, abstract method, and primitive methods.
public abstract class BasePage
{
public void Init()
{
RunAnalytics();
LoadData();
InitControls();
RenderControls();
SetFrames();
}

private void RunAnalytics()
{
//Call firebase analytics
//use common logging
}


//methods to be overriden
protected abstract void LoadData();
protected abstract void InitControls();
protected abstract void RenderControls();
protected abstract void SetFrames();
}

2. Child Pages calling template method and overriding the abstract methods to decide control flow at runtime.

public class UserListPage : BasePage
{
public override void OnAppearing()
{
base.Init();
}
public override void LoadData()
{
Console.WriteLine("Loading data needed for this page");
}
public override void InitControls()
{
//Control Initialization logic
}
public override void RenderControls()
{
//Control rendering logic
}
public override void SetFrames()
{
//set frames logic
}
}
public class OrdersPage : BasePage
{
public override void OnAppearing()
{
base.Init();
}
public override void LoadData()
{
//API Calls to fetch user order list
}
public override void InitControls()
{
//Control Initialisation logic
}
public override void RenderControls()
{
//Control rendering logic
}
public override void SetFrames()
{
//set frames logic
}
}

Takeaway -

To sum this up, a templated design pattern is a great way to organize your code. If your system requires using abstraction and inheritance a lot in your code, it’s always good to take a pause, and weigh things down to see if this design pattern can help in better structuring your code. And then start coding.
If you have more use cases that you think this pattern can help in, please share your thoughts in the comments. Happy coding!

--

--