Content deleted Content added
No edit summary |
→C# example: add image |
||
(17 intermediate revisions by 12 users not shown) | |||
Line 1:
The '''non-virtual interface [[Design pattern (computer science)|
| url = http://www.blackwasp.co.uk/
| title = Non-Virtual Interface Design Pattern
| last1 = Carr
| first1 = Richard
| authorlink1 =
| publisher = BlackWasp
| archiveurl = http://www.blackwasp.co.uk/NVI.aspx
Line 18 ⟶ 16:
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
With such a structure, the [[
| url = http://cpptruths.blogspot.co.at/
| title = Non-Virtual Interface (NVI) idiom and the design intent
| last1 = Tambe
| first1 = Sumant
| authorlink1 =
| publisher = C++ truths
| archiveurl = http://cpptruths.blogspot.co.at/2007/04/non-virtual-interface-nvi-idiom-and.html
| archivedate = 2007-04-11
| 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 ==
== 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]
<!-- Categories -->
[[Category:Software design patterns]]
[[Category:Articles with example Java code]]
[[Category:Method (computer
|