The Template Method Design Pattern is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. It allows reusing common behavior across multiple classes while still allowing customization where necessary.
Related Articles
Components of Template Method Design Pattern
- Abstract Class: This class defines the template method, which is the skeleton of the algorithm. It consists of several abstract methods that subclasses must implement.
- Concrete Classes: These classes inherit from the abstract class and provide implementations for the abstract methods.
Example implementation in C#
Let's consider a scenario where we have a base class Recipe representing a cooking recipe. We want to create different types of recipes such as PizzaRecipe and PastaRecipe. Each recipe will have different steps for preparation, but there will be some common steps like prepareIngredients() and cook().
Create Abstract Template Class (Recipe.cs)namespace TemplateMethodPattern { ////// Abstract class defining the template method /// public abstract class Recipe { // Template method defining the algorithm public void MakeRecipe() { PrepareIngredients(); Cook(); Serve(); } // Abstract methods to be implemented by subclasses protected abstract void PrepareIngredients(); protected abstract void Cook(); // Common method protected void Serve() { Console.WriteLine("Serve the dish."); } } }
PizzaRecipe.cs
namespace TemplateMethodPattern { ///PastaRecipe.cs/// Concrete class implementing the PizzaRecipe /// public class PizzaRecipe : Recipe { protected override void PrepareIngredients() { Console.WriteLine("Prepare pizza dough, sauce, cheese, and toppings."); } protected override void Cook() { Console.WriteLine("Bake the pizza in the oven."); } } }
namespace TemplateMethodPattern { ////// Concrete class implementing the PastaRecipe /// public class PastaRecipe : Recipe { protected override void PrepareIngredients() { Console.WriteLine("Boil pasta and prepare sauce."); } protected override void Cook() { Console.WriteLine("Combine cooked pasta with sauce."); } } }
using TemplateMethodPattern; Recipe pizza = new PizzaRecipe(); Recipe pasta = new PastaRecipe(); Console.WriteLine("Making Pizza:"); pizza.MakeRecipe(); Console.WriteLine("\nMaking Pasta:"); pasta.MakeRecipe(); Console.ReadLine();
Output
Explanation
- In the above example, we have an abstract class Recipe that defines the template method MakeRecipe().
- Subclasses PizzaRecipe and PastaRecipe inherit from the Recipe class and provide implementations for the abstract methods PrepareIngredients() and Cook().
- The MakeRecipe() method orchestrates the process of preparing ingredients, cooking, and serving, but the specific implementation of each step is deferred to subclasses.
- We create instances of PizzaRecipe and PastaRecipe and call the MakeRecipe() method on them, which executes the steps defined in the template method.
The Template Method Design Pattern promotes code reuse and flexibility by allowing subclasses to provide specific implementations while keeping the overall algorithm structure intact. It's useful in scenarios where you have a common algorithm with some variations in implementation across subclasses.
The full source code is available here:
Happy coding!! 😊