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:
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.
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.
leave a comment