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.
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.
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.
template <typename T> T fma ( T a, T b, T c ) { return a + b * c; }
will call the correct specialisations or overloads of T::operator+. –
Permanency As has been said, NVI is a progamming idiom, related to a category of languages. It's been promoted by Herb Sutter among others, because it helps enforcing contracts:
However, the implementation may actually differ considerably, for example another example of NVI implementation is to combine it with Pimpl:
class FooImpl;
class Foo
{
public:
enum type { Type1, Type2 };
Foo(type t, int i, int j);
int GetResult() const;
private:
FooImpl* mImpl;
};
And for the implementation:
struct FooImpl
{
virtual ~FooImpl();
virtual int GetResult() const;
};
class FooType1: public FooImpl
{
public:
FooType1(int i, int j);
virtual int GetResult() const;
private:
/// ...
};
I've always found that it conveyed the point better. Have you figured it out ?
The main point is that virtual
is an implementation detail. And exposing implementation details in the interface is a bad idea, because you may wish to change them.
Furthermore implementation details tend to mess with binary compatibility. For example adding a new virtual
method in a class may change the layout of the virtual table (common implementation technic) and thus botch the binary compatibility. On gcc you need to make sure that you add it last (among the virtual) if you wish to preserve the compatibility.
By using the NVI + Pimpl combination above, there is no virtual
at all (not even private) in the class exposed. The memory layout is backward and forward compatible. We have achieve binary compatibility.
Here, we use several patterns at once:
virtual
function is not with ease of use, it's with ABI. The advantage of FooImpl
being opaque is that you confine it to your binary and thus have no issue of ABI when you deliver new version of the binary to your clients. –
Barney virtual
keyword you provide a hook of customization, which is essentially what the Template Method boils down. So the Template Method is a pattern while NVI is a guideline about the use of the virtual
keyword in C++. –
Barney © 2022 - 2024 — McMap. All rights reserved.