This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Non-Virtual Interface Design Pattern

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.

Adding Code to the Base Class

A key benefit of the non-virtual interface pattern is the ability to add code to the base class that executes before and after the core functionality. This code is then added to all subclasses and cannot be overridden. We can demonstrate this by modifying the Saveable class. In the updated base class below, messages are outputted before and after the call to CoreSave. These represent code that creates and commits a database transaction that controls the data storage.

public abstract class Saveable
    public void Save()
        Console.WriteLine("Creating transaction");
        Console.WriteLine("Committing transaction");

    protected abstract void CoreSave();

The new transaction functionality is automatically added to the Customer subclass. When you run the updated program you can see this in the output:

Creating transaction
Saved customer ABC Limited with credit limit 100000
Committing transaction
3 September 2011