Non-virtual interface pattern: Difference between revisions

Content deleted Content added
Added an article
 
C# example: add image
 
(19 intermediate revisions by 12 users not shown)
Line 1:
The '''non-virtual interface pattern''' ('''NVI''') [[Design pattern (computer science)|design pattern]]''' that('''NVI''') controls how methods in a [[base class]] are overridden,. whichSuch include public, non-virtual members thatmethods may be called by clients, and overridable methods with core functionality.<ref>{{cite web
| url = http://www.blackwasp.co.uk/
| title = Non-Virtual Interface Design Pattern
| last1 = Carr
| first1 = Richard
| authorlink1 = https://profiles.google.com/113882985360066691734| date = 2011-09-03
| date = 2011-09-03
| work =
| publisher = BlackWasp
| archiveurl = http://www.blackwasp.co.uk/NVI.aspx
Line 13 ⟶ 11:
| accessdate = 2012-09-12
}}</ref> 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 ==
 
A design that adheres to this pattern results in a separation of a class interface into two distinct interfaces:
# Client interface: This is the public non-virtual interface
# 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.<ref>{{cite web
| url = http://cpptruths.blogspot.co.at/
| title = Non-Virtual Interface (NVI) idiom and the design intent
| date last1 = 2011-09-03Tambe
| first1 = Sumant
| authorlink1 = | date = 2007-04-11
| publisher = C++ truths
| archiveurl = http://cpptruths.blogspot.co.at/2007/04/non-virtual-interface-nvi-idiom-and.html
| archivedate = 2007-04-11
| work quote =
| accessdate = 2012-09-12
}}</ref>
 
== C# example ==
[[File:Non-virtual interface pattern.svg]]
 
<syntaxhighlight lang="c#" line="1">
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}");
}
}
</syntaxhighlight><ref>{{Cite web|title=Non-Virtual Interface Design Pattern|url=http://www.blackwasp.co.uk/nvi.aspx|access-date=2021-09-19|website=www.blackwasp.co.uk}}</ref><ref>{{Cite web|title=Non-Virtual Interface Design Pattern (Page 2 of 2)|url=http://www.blackwasp.co.uk/nvi_2.aspx|access-date=2021-09-19|website=www.blackwasp.co.uk}}</ref>
 
== See also ==
<!-- Moved from the* [[Template method pattern]] to here -->
 
== References ==
{{Reflist|2}}
 
== External links ==
<!-- Moved from the [[Template method pattern]] to here -->
* [http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface Non-Virtual Interface]
{{software-stub}}
 
<!-- Categories -->
[[Category:Software design patterns]]
 
[[Category:Articles with example Java code]]
<!-- Interwikis -->
[[Category:Method (computer programming)]]