Introducing the Template Method design pattern

Introducing the Template Method design pattern

Read Time : 2 Minutes

Thursday, 11 May 2023

Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Template Method design pattern.

The Template Method pattern is a behavioral design pattern that defines the skeleton of an algorithm in a abstract base class and allows subclasses to implement specific steps of the algorithm without changing its overall structure. This pattern is useful when multiple classes have similar operations but with different implementations.

 

The Template Method design pattern consists of two main components:

  • Abstract Base Class: The base class contains abstract algorithms and methods that are implemented by its subclasses.
  • Concrete Subclasses: Subclasses implement the abstract methods of the base class for specific representations of the algorithm.

Below is an example of the Template Method design pattern in C# language:

public abstract class Pizza
{
    public void Make()
    {
        PrepareDough();
        AddSauce();
        AddToppings();
        Bake();
    }
    protected abstract void PrepareDough();
    protected abstract void AddSauce();
    protected abstract void AddToppings();
    protected virtual void Bake()
    {
       Console.WriteLine("Baking pizza at 400 degrees for 20 minutes.");
    }
}
public class PepperoniPizza : Pizza
{
    protected override void PrepareDough()
    {
       Console.WriteLine("Preparing thick crust dough.");
    }
    protected override void AddSauce()
    {
       Console.WriteLine("Adding tomato sauce.");
    }
    protected override void AddToppings()
    {
       Console.WriteLine("Adding pepperoni, cheese, and mushrooms.");
    }
}
public class MargheritaPizza : Pizza
{
    protected override void PrepareDough()
    {
       Console.WriteLine("Preparing thin crust dough.");
    }
    protected override void AddSauce()
    {
        Console.WriteLine("Adding marinara sauce.");
    }
    protected override void AddToppings()
    {
       Console.WriteLine("Adding mozzarella cheese and basil.");
    }
}
public class Program
{
    public static void Main()
    {
        Pizza pepperoniPizza = new PepperoniPizza();
        pepperoniPizza.Make();
       Console.WriteLine();
        Pizza margheritaPizza = new MargheritaPizza();
        margheritaPizza.Make();
    }
}

In this example, the Pizza abstract class defines the general algorithm for making a pizza with the abstract methods PrepareDough, AddSauce, and AddToppings. The Bake method has a default implementation, but can be overridden in subclasses if necessary. The PepperoniPizza and MargheritaPizza subclasses implement abstract methods for their own renderings of pizza making. Finally, the Main method creates two different instances, both of which are created by calling the abstract Bake method.

The Template Method pattern provides a way to define a common algorithm in a base class and allows subclasses to implement specific steps while maintaining the overall structure of the algorithm. This pattern enables code reuse and flexibility in performing similar operations.

To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.

design patterns

Creational:Factory Design Pattern
Builder Design Pattern
Singleton Design Pattern
Prototype Design Pattern
Abstract Factory Design Pattern
Structural:Adapter Design Pattern
Bridge Design Pattern
Composite Design Pattern
Decorator Design Pattern
Facade Design Pattern
Flyweight Design Pattern
Proxy Design Pattern
Behavioral:Chain of Responsibility Design Pattern
Command Design Pattern
Interpreter Design Pattern
Mediator Design Pattern
Memento Design Pattern
Observer Design Pattern
State Design Pattern
Strategy Design Pattern
Template Method Design Pattern
Visitor Design Pattern
Iterator Design Pattern
  • Share:
reza babakhani
Reza Babakhani

I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.

Latest post

Service Mesh Simplifying Microservice Communication

In the ever-evolving landscape of software development, microservices architecture has gained considerable popularity due to its scalability, flexibility, and extensibility. However, as the number of microservices in an application increases, it becomes increasingly challenging to manage their communication and ensure that they are all properly visible. This is where Service Mesh comes into play.

The Importance of Edge Computing

Due to the speed of technological evolution, one of the concepts that has attracted a lot of attention and changes the way we interact with digital systems is edge computing.

What is event-driven architecture?

Event-driven architecture (EDA) is a software design pattern that has become increasingly popular in modern software development. In this architecture, the flow of data is determined by the occurrence of events. Unlike traditional centralized systems that are constantly checking for new status. Event-driven architecture is especially useful for systems that need to process large amounts of data in real-time.

leave a comment