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 feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos