1

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!

Jeff
  • 3,475
  • 4
  • 26
  • 35

4 Answers4

5

Could be a template method pattern.

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
  • 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 Jul 22 '10 at 14:17
  • I don't believe that it does differ. In fact, the link you just posted calls it an alias. – Steven Sudit Jul 22 '10 at 14:17
  • Nevermind, Sutter coined NVI as a subset of TMP: http://www.gotw.ca/publications/mill18.htm (in first 'Later note:' section) – Jeff Jul 22 '10 at 14:21
  • 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 Jul 22 '10 at 14:29
  • Don't worry about the points. All that matters is that the correct answer(s) can be recognized. – Steven Sudit Jul 22 '10 at 14:32
  • 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 Jul 22 '10 at 14:39
  • I made the changes you requested, since it's your question. I should mention that both of those tags were new. – Steven Sudit Jul 22 '10 at 14:42
  • 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 Jul 22 '10 at 14:45
3

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
  • 39,212
  • 14
  • 67
  • 75
  • 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 Jul 22 '10 at 14:27
1

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
  • 219,715
  • 46
  • 258
  • 445
  • This is actually the most precise answer in retrospect. It just missed the rush of up votes from arriving slightly later. Thanks, sbi! – Jeff Jul 22 '10 at 14:55
  • @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 Jul 22 '10 at 15:16
0

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
  • 17,166
  • 1
  • 38
  • 51