Template method

Introduction:

Template method is a behavioral design pattern that defines the skeleton of an algorithm in the super class. Subclasses override specific steps of the algorithm without changing its structure or order. The parent class maintains the algorithm's basic framework as well as sequencing.

Template method is based on inheritance, and its works in class level. So its static.

When to use, 

Different components have significant similarities, but demonstrate no reuse of common interface or implementation. If a change common to both components becomes necessary, duplicate effort must be expended.

How to use, 

  1. Examine the algorithm, and decide which steps are standard and which steps are peculiar to each of the current classes.
  2. Define a new abstract base class.
  3. Move the shell of the algorithm (now called the "template method") and the definition of all standard steps to the new base class.
  4. Define a placeholder or "hook" method in the base class for each step that requires many different implementations. This method can host a default implementation or pure virtual (C++).
  5. Invoke the hook method(s) from the template method.
  6. Each of the existing classes declares an "is-a" relationship to the new abstract base class.
  7. Remove from the existing classes all the implementation details that have been moved to the base class.
  8. The only details that will remain in the existing classes will be the implementation details peculiar to each derived class.

UML Class Diagram:








Example, 

Define a Sort algorithm framework since supports different sorting methods like Bubble sort, Quick sort, Selection sort etc., 

Implementation:

1. Define template class (CSort class)
2. Define TemplateMethod in template class. This method has steps in order.
3. Define invariant implementation in base class.
4. Define pure virtual function in template class which needs variant implementation for different sorting algorithm.
5. Define override function in derived class (each sorting class).

Class Diagram:









Sourcecode: https://github.com/krishnaKSA/design_patterns_cplusplus/blob/main/TemplateMethod/TemplateMethod.hpp

Template class/Base class:

Template class has template method "sortArray", this defines the order of steps involved in the particular algorithm. It has invariant algorithm steps implementation as default implementation.

    class CSort
    {    
        void processArray()
        {
            cout<<"CSort: "<<__FUNCTION__<<endl;
        }
       
        void printArray()
        {
            cout<<"CSort: "<<__FUNCTION__<<endl;
        }

        public:
        virtual void sortArray() final
        {
            processArray();
            compare();
            printArray();
        }

        virtual void compare() = 0;        
    };

Subclass:

Defined derived classes for quick sort, bubble sort, selection sort which is derived from the base/template class. These subclass has implementation for variant steps of the algorithm.

    class CBubbleSort :  public CSort
    {
        public:
        void compare()
        {
            cout<<"CBubbleSort: "<<__FUNCTION__<<endl;
        }

    };
    class CSelectionSort :  public CSort
    {
        public:
        void compare()
        {
            cout<<"CSelectionSort: "<<__FUNCTION__<<endl;
        }
    };
    class CQuickSort :  public CSort
    {
        public:
        void compare()
        {
            cout<<"CQuickSort: "<<__FUNCTION__<<endl;
        }
    };

Client code:

    void testTemplateMethod()
    {
        CSort* qSort = new CQuickSort;
        qSort->sortArray();
        CSort* bSort = new CBubbleSort;
        bSort->sortArray();
        CSort* sSort = new CSelectionSort;
        sSort->sortArray();
    }