The template method design pattern allows you to define the skeleton of an algorithm in a base class and defer the details to subclasses Credit: Thinkstock Design patterns are proven solutions to recurring problems and complexities in software development. Design patterns fall into three categories: creational, structural, and behavioral. Creational patterns are used to create and manage instances of classes. Structural patterns are used to realize the relationships among the entities. Behavioral design patterns deal with object collaboration and delegation of responsibilities. Note that the template method pattern belongs to the behavioral design pattern category. In the sections that follow, we will examine this design pattern in detail. Why use the template method pattern? The template method design pattern is useful when you have a skeleton of an algorithm defined in a base class and a small part of the algorithm may vary and is implemented with variation in a subclass, i.e., the concrete class. In essence, the template of the algorithm is defined in the base class and while certain steps of the algorithm (the part that variesthose that vary from the base class) is are re-defined in a concrete class. The Gang of Four defines the template method pattern as follows: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. In other words, the template method lets you define a group of interchangeable, similarly structured, multi-step algorithms in your application. Note that each of these algorithms adheres to the same sequence of steps but has a different implementation. The base class defines the structure of the algorithm, while the derived classes define variations on the algorithm without changing its structure. As the name of the pattern suggests, the base class serves as a kind of template or placeholder. The template method pattern in action Let’s examine how this design pattern can be implemented. The typical participants in the template method pattern are an abstract class and a concrete class. Consider the class below, which contains two kinds of methods. One defines each step of an algorithm, while the other is a template method that controls the algorithm and invokes the steps. public abstract class AlgorithmBase { public void TemplateMethod() { OperationA(); OperationB(); OperationC(); } public abstract bool OperationA(); public abstract bool OperationB(); public abstract bool OperationC(); } As you can see, the abstract class AlgorithmBase defines a TemplateMethod and three abstract methods that carry out the steps in the algorithm. And here’s the concrete class for your reference: public sealed class AlgorithmA : AlgorithmBase { public override bool OperationA() { Console.WriteLine(“Inside OperationA() of AlgorithmA”); return true; } public override bool OperationB() { Console.WriteLine(“Inside OperationB() of AlgorithmA”); return true; } public override bool OperationC() { Console.WriteLine(“Inside OperationC() of AlgorithmA”); return true; } } The AlgorithmA class extends the AlgorithmBase class and implements each of the abstract methods. Similarly, you could have another concrete class that extends the AlgorithmBase class and implements each of the abstract methods its own way. For instance: public sealed class AlgorithmB : AlgorithmBase { public override bool OperationA() { Console.WriteLine(“Inside OperationA() of AlgorithmB”); return true; } public override bool OperationB() { Console.WriteLine(“Inside OperationB() of AlgorithmB”); return true; } public override bool OperationC() { Console.WriteLine(“Inside OperationC() of AlgorithmB”); return true; } } Finally, the following code snippet shows how you can invoke the template methods on instances of the AlgorithmA and AlgorithmB classes. static void Main(string[] args) { AlgorithmBase obj1 = new AlgorithmA(); obj1.TemplateMethod(); AlgorithmBase obj2 = new AlgorithmB(); obj2.TemplateMethod(); Console.Read(); } The template method design pattern allows you to implement a general algorithm while leaving room for easy customization. You will likely find it useful again and again. Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos