Skip to Content
Hands-On Design Patterns with C++
book

Hands-On Design Patterns with C++

by Fedor G. Pikus
January 2019
Intermediate to advanced
512 pages
14h 5m
English
Packt Publishing
Content preview from Hands-On Design Patterns with C++

The Template Method in C++

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Structural Design Patterns in Modern C++

Structural Design Patterns in Modern C++

Umar Lone

Publisher Resources

ISBN: 9781788832564Supplemental Content