Non-virtual interface pattern

The non-virtual interface pattern (NVI) controls how methods in a base class are overridden.

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

The non-virtual interface pattern (NVI) controls how methods in a base class are overridden. Such methods may be called by clients and overridable methods with core functionality.[1] It is a pattern that is strongly related to the template method pattern. The NVI pattern recognizes the benefits of a non-abstract method invoking the subordinate abstract methods. This level of indirection allows for pre and post operations relative to the abstract operations both immediately and with future unforeseen changes. The NVI pattern can be deployed with very little software production and runtime cost. Many commercial software frameworks employ the NVI pattern.

Benefits and detriments

edit edit source

A design that adheres to this pattern results in a separation of a class interface into two distinct interfaces:

  1. Client interface: This is the public non-virtual interface
  2. Subclass interface: This is the private interface, which can have any combination virtual and non-virtual methods.

With such a structure, the fragile base class interface problem is mitigated. The only detriment is that the code is enlarged a little.[2]

C# example

edit edit source

public abstract class Saveable { // The invariant processing for the method is defined in the non virtual interface. // The behaviour so defined is inherited by all derived classes. // For example, creating and committing a transaction. public void Save() { Console.WriteLine("Creating transaction"); CoreSave(); Console.WriteLine("Committing transaction"); } // The variant processing for the method is defined in the subclass interface. // This behaviour can be customised as needed by subclasses. // For example the specific implementation of saving data to the database. protected abstract void CoreSave(); } public class Customer : Saveable { public string Name { get; set; } public decimal Credit { get; set; } protected override void CoreSave() { Console.WriteLine($"Saved customer {Name} with credit limit {Credit}"); } }

[3][4]

See also

edit edit source

References

edit edit source
  1. ^ Carr, Richard (2011-09-03). "Non-Virtual Interface Design Pattern". BlackWasp. Archived from the original on 2011-09-25. Retrieved 2012-09-12. The non-virtual interface pattern is a design pattern that controls how methods in a base class are overridden. Base classes include public, non-virtual members that may be called by clients and a set of overridable methods containing core functionality. Wikipedia revision 1338901995, retrieved 2026-11-19
  2. ^ Tambe, Sumant (2007-04-11). "Non-Virtual Interface (NVI) idiom and the design intent". C++ truths. Archived from the original on 2013-09-26. Retrieved 2012-09-12.{{cite web}}: CS1 maint: bot: original URL status unknown (link) Wikipedia revision 1338901995, retrieved 2026-11-19
  3. ^ "Non-Virtual Interface Design Pattern". www.blackwasp.co.uk. Archived from the original on September 25, 2011. Retrieved 2021-09-19. Wikipedia revision 1338901995, retrieved 2026-11-19
  4. ^ "Non-Virtual Interface Design Pattern (Page 2 of 2)". www.blackwasp.co.uk. Archived from the original on October 19, 2011. Retrieved 2021-09-19. Wikipedia revision 1338901995, retrieved 2026-11-19
edit edit source

Text derived from https://en.wikipedia.org/w/index.php?oldid=1338901995 under CC BY-SA 4.0; modifications have been made.