views:

78

answers:

4

What's the accepted jargon (if any) for describing methods meant to be invoked only virtually and from other methods in the base? I've occasionally seen this referred to as a callback, but that seems to stray pretty far from the original definition of that term. I'm not even sure that this merits being called a pattern, but I'm trying to become more precise in commenting my code. Thanks for the help!

// Abstract class.
class A {
public:
  void run() { while (call()) { /* ... */ } }
private:
  virtual bool call() = 0;
};

// Completion/specialization of A.
class B : public A {
private:
  // Standard term to indicate this pattern?
  bool call();
};

Summary: This appears to be called the Non-Virtual Interface pattern, a special case of the Template Method Pattern. Thanks to Nick and Steven for the help!

+5  A: 

Could be a template method pattern.

Steven Sudit
Thanks for the link. Could you explain more clearly than the article how this differs, if at all from NVI? At least once source I dug up claims that they are synonymous: http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface
Jeff
I don't believe that it does differ. In fact, the link you just posted calls it an alias.
Steven Sudit
Nevermind, Sutter coined NVI as a subset of TMP:http://www.gotw.ca/publications/mill18.htm (in first 'Later note:' section)
Jeff
Anyway, it appears that NVI is the more precise term for the TMP case that I'm documenting. I had to give credit to Nick, but I'd vote you up twice if I could. Thanks for the help!
Jeff
Don't worry about the points. All that matters is that the correct answer(s) can be recognized.
Steven Sudit
Anyway, I lack sufficient reputation to make new tags for this question. Could you possibly make 'non-virtual-interface' and 'template-method-pattern' tags and change this question's tags to "template-method-pattern non-virtual-interface design-patterns virtual private"?
Jeff
I made the changes you requested, since it's your question. I should mention that both of those tags were new.
Steven Sudit
I would have changed to them had they already existed, but I'm still too far a peon to make new ones. Since they are the most precise terms for the issue, I hope they survive despite their apparent obscurity. Thanks again!
Jeff
A: 

Um... private virtuals? Why invent new terminology? It's a language construct, not an idiom, and to my mind not interesting enough to be termed a pattern.

Pontus Gagge
-1: Yes, private virtuals. C++ allows them and they're common for implementing the template and NVI patterns.
Billy ONeal
@Billy: were you agreeing or disagreeing?
Pontus Gagge
@Pontus: Removed downvote as a result of the edit. If you only have the original it looks like you're calling the OP stupid :P.
Billy ONeal
+3  A: 

This is sometimes called the "non-virtual interface" (or NVI) pattern. It is often used when the implementation for the virtual function needs to vary between derived classes, but the base class needs control over when the function is called.

For example, the base class could make another function call before or after the virtual call instead of making the virtual function public and relying on overrides to call the base implementation themselves (and at the right time!)

Nick Meyer
I think this is it, given the head start that Steven pointed me to. NVI appears to be a special subset of the Template Method Pattern, coined by Herb Sutter sometime after 2001.
Jeff
+1  A: 

I've heard the pattern where you don't have any virtual functions in your interface as the Non-Virtual Interface pattern, NVI for short.

In other contexts it's referred to as the Template Method pattern, where your run() is a template method, with derived classes jumping in to fill in the gaps.

sbi
This is actually the most precise answer in retrospect. It just missed the rush of up votes from arriving slightly later. Thanks, sbi!
Jeff
@Jeff: Thanks for the praise, but I was less than a minute later than the first answer. People just considered the other answers better.
sbi