Non-virtual interface pattern: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
m Removed parameters. | You can use this bot yourself. Report bugs here. | Activated by User:Neko-chan | Category:Software design patterns | via #UCB_Category
C# example: add image
 
(6 intermediate revisions by 3 users not shown)
Line 30:
| 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 ==
* [[Template method pattern]]
 
== References ==