Re: Am I using NVI?
IMO both books are right ;-), they address two different situations,
that's all.
Pure virtual interfaces are very good for "design by contract". e.g. a
plug-in-type class: server defines an interface, plug-in does what it
wants within the realm of the interface contract spec.
Nonpublic virtuals are good as extensibility points with
implementation inheritance. E.g. in a class X, method Y does Z. It is
desirable, in some situations, that some processing occur before or
after Z. That is best solved through e.g.
void ZComplete()
{
PreZ(); // virtual
Z();
PostZ(); // virtual
}
I can't see a clear-cut line between the two approaches. Evolution of
code shows best which to choose. E.g. one might start off with
implementation inheritance and PreZ/PostZ. Then, after two years, one
realizes that PreZ and PostZ are more often used than not. In that
case, pure virtual interface and composition is a better solution,
e.g.
void ZComplete()
{
GetZDecoration().PreZ();
Z();
GetZDecoration().PostZ();
}
// obtained through X::GetZDecoration()
struct /*interface*/ ZDecoration
{
virtual void PreZ() = 0;
virtual void PostZ() = 0;
}
HTH,
Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]