January 2019
Intermediate to advanced
512 pages
14h 5m
English
The Template Method pattern is easily implemented in any object-oriented language. The C++ implementation uses inheritance and virtual functions. Note that this has nothing to do with C++ templates, as in generic programming. The template here is the skeleton implementation of the algorithm:
class Base { public: bool TheAlgorithm() { if (!Step1()) return false; // Step 1 failed Step2(); return true; }};
The template here is the structure of the algorithm—all implementations must first do Step one, which may fail. If this happens, the entire algorithm is considered to have failed, and nothing more is done. If Step one succeeded, we must do Step two. By design, Step two cannot fail, and the overall algorithm computation ...