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.
- The invariant parts of a behavior should be implemented only once so that subclasses can implement the variant parts.
- Subclasses should redefine only certain parts of a behavior without changing the other parts.
Usually, subclasses control how the behavior of a parent class is redefined, and they aren't restricted to redefine only certain parts of a behavior.
The Template Method pattern suggests to break down an alogirthm into a series of methods and call them one by one inside a single template method.
- Define abstract operations (primitives) for the variant parts of a behavior.
- Define a template method that
- implements the invariant parts of a behavior.
- calls abstract operations (primitives) that subclasses implement.
The template method controls how subclasses redefine a behavior. This is also referred to as inversion of control because subclasses do no longer control how the behavior of a parent class redefined.
- AbstractClass (CaffeineBeverage)
- defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm
- implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
- ConcreteClass (Tea, Coffee)
- implements the primitive operations ot carry out subclass-specific steps of the algorithm
- ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm.
- Helps to eliminate code duplication
- You are limited with a skeleton of an existing algorithm
- Template methods tend to be harder to maintain the more steps they have.
- When subclasses should be able to extend the base algorithm without altering its structure.
- When you have several classes that do similar things with only minor differences. When you alter one of the classes, you have to change others as well (e.g. HTML/PDF/CSV Generator follows the same data processing steps before generating the specific file format).
Definition
publicabstractclassCaffeineBeverage{// Template Method// It serves as a template for an algorithm.// In this case, an algorithm for making caffeinated beverages.publicvoidPrepareRecipe(){BoilWater();Brew();// abstract -> handled by subclassPourInCup();AddCondiments();// abstract --> handled by subclass}publicabstractvoidBrew();publicabstractvoidAddCondiments();publicvoidBoilWater(){Console.WriteLine("Boiling water");}publicvoidPourInCup(){Console.WriteLine("Pouring in a cup");}}Usage
publicclassTea:CaffeineBeverage{publicoverridevoidBrew(){Console.WriteLine("Steeping the tea");}publicoverridevoidAddCondiments(){Console.WriteLine("Adding Lemon");}}publicclassCoffee:CaffeineBeverage{publicoverridevoidBrew(){Console.WriteLine("Dripping Coffee through filter");}publicoverridevoidAddCondiments(){Console.WriteLine("Adding Sugar and Milk");}}Factory Method are often called by TemplateMethod.
Strategy - Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.

