In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. in the book Design Patterns. The template method is a method in a superclass, usually an abstract superclass, and defines the skeleton of an operation in terms of a number of high-level steps. These steps are themselves implemented by additional helper methods in the same class as the template method.
The helper methods may be either abstract methods, in which case subclasses are required to provide concrete implementations, or hook methods, which have empty bodies in the superclass. Subclasses can (but are not required to) customize the operation by overriding the hook methods. The intent of the template method is to define the overall structure of the operation, while allowing subclasses to refine, or redefine, certain steps.
Overview
This pattern has two main parts:
- The "template method" is implemented as a method in a base class (usually an abstract class). This method contains code for the parts of the overall algorithm that are invariant. The template ensures that the overarching algorithm is always followed.]]
In the above UML class diagram, the <code>AbstractClass</code> defines a <code>templateMethod()</code> operation that defines the skeleton (template) of a behavior by
- implementing the invariant parts of the behavior and
- sending to self the messages <code>primitive1()</code> and <code>primitive2()</code> , which, because they are implemented in <code>SubClass1</code> , allow that subclass to provide a variant implementation of those parts of the algorithm.
thumb|none|300px|Template Method in [[Lepus3|LePUS3.]]
Usage
The template method is used in frameworks, where each implements the invariant parts of a domain's architecture, while providing hook methods for customization. This is an example of inversion of control. The template method is used for the following reasons.
- It lets subclasses implement varying behavior (through overriding of the hook methods).
- It avoids duplication in the code: the general workflow of the algorithm is implemented once in the abstract class's template method, and necessary variations are implemented in the subclasses.
C++ example
This C++23 implementation is based on the pre C++98 implementation in the book.
Cpp template method pattern UML.svg
<syntaxhighlight lang="c++">
import std;
using std::unique_ptr;
// abstract class
class View {
private:
void setFocus() {
std::println("View::setFocus called");
}
void resetFocus() {
std::println("View::resetFocus called");
}
public:
// defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
virtual void doDisplay() = 0;
// 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.
void display() {
setFocus();
doDisplay();
resetFocus();
}
virtual ~View() = default;
};
// concrete class
class MyView : public View {
public:
// implements the primitive operations to carry out subclass-specific steps of the algorithm.
void doDisplay() override {
// render the view's contents
std::println("MyView::doDisplay called");
}
};
int main(int argc, char* argv[]) {
unique_ptr<View> myview = std::make_unique<MyView>();
myview->display();
}
</syntaxhighlight>
The program output is
<syntaxhighlight lang="c++">
View::setFocus called
MyView::doDisplay called
View::resetFocus called
</syntaxhighlight>
See also
- Inheritance (object-oriented programming)
- Method overriding (programming)
- GRASP (object-oriented designer)
- Adapter pattern
- Strategy pattern
References
External links
- Six common uses of the template pattern
- Template Method Design Pattern
