[Qt-interest] Non-Virtual Interface Idiom
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Mon Dec 20 10:34:10 CET 2010
On 2010-12-19 Piotr Piotr Dobrogost wrote:
> One of design rules made popular by Herb Sutter is Non-Virtual Interface Idiom
> explained in the article "Virtuality" in C/C++ Users Journal, 19(9), September 2001
> (http://www.gotw.ca/publications/mill18.htm), which can be summarized in the
> guideline "Prefer to make interfaces nonvirtual, using Template Method."
I think Herb Sutter did not understand the point of interfaces: he mixes implementation with interfaces. When I talk about "interfaces" I think "Java interfaces", and in C++ that renders to pure virtual classes (in my understanding of "interfaces").
That means having those methods public AND virtual (means: YES, you HAVE to implement this) is the whole point of interfaces! To Herb Sutter this is a "simultaneously" problem: "It's specifying the interfaces" [yes, because that's what an interface is supposed to do] "... and it's specifying implementation detail, namely the internally customizable behavior, because it's virtual and therefore provides a hook for derived classes to replace the base implementation of that function" [and YES, that's exactly what an interface is SUPPOSED to tell the implementator: please implement this by overriding].
So in an interface there should never be anything else other than public pure virtual methods! Maybe protected methods (but I agree they would already expose implementation details), but for sure no private part!
What Herb Sutter actually wants to promote is the Template Pattern, which is good when we talk about the "fragile base class problem", that is you want to control exactly WHAT a subclass is supposed to override and WHEN this is called from the base class. Qt does this as well, all the event handler methods as an example: the specific classes override them and the Qt event queue makes sure they are called at the appropriate time. Same for the paint etc. methods.
But for interfaces this totally does not make sense to me, and the fact that this practise has had no impact since 2001 (the time this article was published) tells me that others agree ;)
Just my $0.02 ;)
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list