c++design-patternstemplate-method-patternnon-virtual-interface

C++: Difference between NVI and Template Method Patterns?


What is the difference between NVI ( Non-Virtual Interface ) and the Template Method patterns?

They seem very similar and I've read both that they're basically the same and that they're subtly different with Template being somehow more general.


Solution

  • NVI is an idiom, Template Method is a pattern. NVI is an implementation of the Template Method Pattern using dynamic dispatch in C++; it is also possible to create template methods in C++ using template metaprogramming to eliminate the dynamic dispatch.

    A pattern is more general than an idiom, and languages may use different idioms to implement the pattern.