[Qt-interest] Non-Virtual Interface Idiom
Andreas Pakulat
apaku at gmx.de
Mon Dec 20 08:25:44 CET 2010
On 20.12.10 01:55:28, Robert Escott wrote:
> Hi Piotr
>
> I've read the article through a number of times and quite frankly, I don't
> really understand what new methodology this guy's talking about.
>
> I don't feel he's argued well enough against the use of public or protected
> virtual functions. He mentions the "problem of simultaneity" but I fail to
> see why this is a problem at all and the guy doesn't explain it either. He
> seems to feel that having public or protected virtual functions somehow
> destabilises the class interface but doesn't say how.
>
> If you want rationale behind Qt's decision not to follow the "Non-virtual
> interface" paradigm, just read any book on OOP and polymorphism. It's all
> there.
>
> Take Qt's Model-View architecture, for example. Views all need to access
> data from the various model classes yet the Models need to be customisable
> via derived classing. This means that the QVariant data() function has to be
> both virtual and public.
No it doesn't. Thats the point of the article, you can have a public
QVariant data() function that is non-virtual. This is the interface to
the outside. For subclasses the interface would be a private virtual
function fetchData() (or some other dummy name), which is being called
from data().
> Also, all QWidgets and their derived classes must be able to process QEvents
> or their derivatives. This means that the event function needs to be
> virtual.
Sure, but it doesn't mean it has to be public. Or rather it doesn't mean
the overrideable part has to be public.
Anyway, I what I don't like about this are tons of function-duplication.
IMHO its pretty silly to have a class which has a data() function and a
doData() virtual just for overriding. Or even 2
doDataPhase1&doDataPhase2. And of course it also means you cannot
override certain aspects of the actual data() implementation and hence
the concept limits the usability of classes designed this way.
So the only benefit is helping people not shooting their own foot by
preventing them from breaking certain contracts defined by the interface
class.
Andreas
--
Save energy: be apathetic.
More information about the Qt-interest-old
mailing list