Template Method Design Pattern

This has nothing to do with Template (C++), HOWEVER CRTP has to do with Template (C++).

From CS247, Lecture 18 Turtle example.

Template Method is a behavioural 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.

Template Method Pattern, which is a behavioural design pattern that defines the structure of an algorithm in a base class but allows subclasses to provide specific implementations for certain steps of the algorithm. The key idea is that the overall algorithm is defined in the base class, while the specifics of individual steps are left to subclasses.

Wikipedia example: https://en.wikipedia.org/wiki/Template_method_pattern

Resource: https://refactoring.guru/design-patterns/template-method

Some of my understanding

You provide a ā€œtemplateā€ for what the method will do and subclasses customize portions of this method by overriding.

Pros: Eliminate code duplication for certain similar steps for all of them

The Template Method pattern suggests that you break down an algorithm into a series of steps, turn these steps into methods, and put a series of calls to these methods inside a single template method. The steps may either be abstract, or have some default implementation. To use the algorithm, the client is supposed to provide its own subclass, implement all abstract steps, and override some of the optional ones if needed (but not the template method itself).

Example:

Template Method Pattern: In video game, I have two types of Turtles: Red Turtle + Green Turtle

class Turtle {
	virtual void drawShell()=0;
	virtual void drawHead(){šŸ˜„;}
	virtual void drawLegs(){šŸ¦µšŸ¦µšŸ¦µšŸ¦µ;}
	public:
		void draw(){
			drawHead();
			drawShell();
			drawLegs();
		}
};
 
class RedTurtle: public Turtle{
	void drawShell() override {šŸ”“;}
};
 
class GreenTurtle: public Turtle{
	void drawShell(){šŸŸ¢;f}
}

Note: draw method is public and non-virtual.

Nobody who subclasses Turtle can override draw.

Note

But whenever a Turtle calls draw(), it will always call from the base class, and notice how the other function drawHead, drawShell, and drawLegs are virtual.

(GreenTurtle and RedTurtle can define draw, but it wonā€™t be an override, wonā€™t be called if we use Turtleā€™s polymorphically.) Which makes total sense, since we didnā€™t specify virtual for the draw() method.

Note as well: drawShell() is private, but we can still override it! Access specifiers (public, protected, private) only describe when methods can be called, not how they may be overwritten. So it sill can be overridden.

Structure

  1. The Abstract Class declares methods that act as steps of an algorithm, as well as the actual template method which calls these methods in a specific order. The steps may either be declared abstract or have some default implementation.
  2. Concrete Classes can override all of the steps, but not the template method itself.

Usually, the templateMethod() will do things that are similar to everyone and then call the virtual functions inside. Which will be decided polymorphically during runtime.


From CS138.

Very simple example of the Template Method design pattern:

  • The parent has a high-level recipe that is the same for all children
  • ā€¦ but the recipe has sub-pieces whose details will be different depending on the details of the children
  • The parent method is (typically) public, while the recipe sub-pieces are private and abstract and declared in the parent
  • The children figure out how to implement the recipe sub-pieces; the parent specifies the rest.

Example

  • Non-virtual Idiom