}}</ref>
=== C# Example ===
<syntaxhighlight lang="c#" line="1">
public abstract class Saveable
namespace Shell
{
public classvoid Node<TData>Save()
{
public TData Data { getCoreSave(); set; }
public Node<TData> Next { get; set; } ▼
public Node() { }
public Node(TData data)
{
Data = data;
}
public override string ToString() ▼
{
return Data.ToString();
}
}
protected abstract void CoreSave();
public class DoubleNode<TData>: Node<TData>
}
{
public new DoubleNode <TData> Next { get; set; } ▼
public DoubleNode <TData> Previous { get; set; }
public class Customer : Saveable
{
public DoubleNode () { }
▲ public Node<TData>string NextName { get; set; }
public DoubleNode (TData data): base(data) {}
▲ public new DoubleNode <TData>decimal NextCredit { get; set; }
}
▲ publicprotected override stringvoid ToStringCoreSave()
public class LinkedList<TData, TNode> where TNode: Node<TData>
{
public TNode Head { get; internal set; }
public TNode Tail { get; internal set; }
public LinkedList()
{
Head = Tail = null;
}
// Non virtual interface pattern
// We define `protected virtual` methods that contain the variant implementations of the linked list API.
// The invariant processing necessary for the API is implemented in our `public` methods.
// The variant processing necessary to implement the API can be implemented by the derived classes in the hook methods.
protected virtual void VariantAddHeadNode(TNode newHead) { }
public void AddHeadNode(TNode newHead)
{
if (Head is null)
Tail = newHead;
else
{
VariantAddHeadNode(newHead);
newHead.Next = Head;
}
Head = newHead;
}
}
public class DoublyLinkedList<TData>: LinkedList<TData, DoubleNode<TData>>
{
Console.WriteLine("Saved customer {0} with credit limit {1}", Name, Credit);
public DoublyLinkedList(): base() {}
// The variant processing necessary to implement the API in a doubly linked list.
protected override void VariantAddHeadNode(DoubleNode<TData> newHead)
{
Head.Previous = newHead;
}
}
}
</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>
</syntaxhighlight>
== See also ==
|