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.
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 { /// <summary> /// Abstract class defining the template method /// </summary> 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 { /// <summary> /// Concrete class implementing the PizzaRecipe /// </summary> 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."); } } }PastaRecipe.cs
namespace TemplateMethodPattern { /// <summary> /// Concrete class implementing the PastaRecipe /// </summary> 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!! 😊
No comments:
Post a Comment