C++ Template Method Design Pattern Example

1. Definition

The Template Method Design Pattern defines the skeleton of an algorithm in a method in an algorithm class but delays some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

2. Problem Statement

Imagine you're creating a data processing application where multiple algorithms process data but share some common steps. If you replicate these steps in each algorithm, it can lead to code duplication and difficulties when making changes or updates.

3. Solution

Decompose the algorithm into a series of steps, and define the unchanging steps in a base class while leaving the varying steps abstract for subclasses to provide concrete implementations.

4. Real-World Use Cases

1. A cooking guide where the steps for preparing and serving are same, but the cooking method varies (e.g., bake, fry, steam).

2. Software builds systems where the sequence of processes like code compilation, testing, and deployment remains consistent, but the specifics of each step might vary.

5. Implementation Steps

1. Create an abstract base class with a method (often called a 'template method') that defines the structure of the algorithm.

2. Implement the invariant steps of the algorithm in the base class.

3. Declare abstract methods in the base class for variant steps.

4. Subclasses should provide concrete implementations for these abstract methods.

6. Implementation in C++

// Abstract Class
class DataProcessor {
public:
    // Template method
    void process() {
        readData();
        processData();
        saveData();
    }
    virtual ~DataProcessor() {}
protected:
    virtual void readData() = 0;
    virtual void processData() = 0;
    void saveData() {
        // Implementation of saving data (This remains consistent across all algorithms)
        std::cout << "Data Saved." << std::endl;
    }
};
// Concrete Class
class ExcelDataProcessor : public DataProcessor {
protected:
    void readData() override {
        std::cout << "Reading data from Excel file." << std::endl;
    }
    void processData() override {
        std::cout << "Processing Excel data." << std::endl;
    }
};
// Example Usage
int main() {
    ExcelDataProcessor processor;
    processor.process();
    return 0;
}

Output:

Reading data from Excel file.
Processing Excel data.
Data Saved.

Explanation:

1. The DataProcessor class has a method process, which is our template method. This method outlines the sequence of steps required to process data.

2. Among these steps, saveData is common and is implemented in the base class, while readData and processData are left abstract.

3. ExcelDataProcessor provides concrete implementations for the abstract methods.

4. When processor.process() is called, it executes the steps in the order defined by the template method.

7. When to use?

Use the Template Method pattern when:

1. The invariant parts of an algorithm are in one place and it's mandatory for the subclasses to execute them.

2. Common behaviors among subclasses should be centralized to avoid duplication.

3. You want to control the extension points of an algorithm by subclasses.


Comments