Non-virtual Interface Pattern
   HOME

TheInfoList



OR:

The non-virtual interface
pattern A pattern is a regularity in the world, in human-made design, or in abstract ideas. As such, the elements of a pattern repeat in a predictable manner. A geometric pattern is a kind of pattern formed of geometric shapes and typically repeated l ...
(NVI) controls how methods in a
base class In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new classe ...
are overridden. Such methods may be called by clients and overridable methods with core functionality. It is a pattern that is strongly related to the
template method pattern In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. in the book ''Design Patterns''. The template method is a method in a superclass, usually an abstract superclass, and defines ...
. 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 The fragile base class problem is a fundamental architectural problem of object-oriented programming systems where base classes ( superclasses) are considered "fragile" because seemingly safe modifications to a base class, when inherited by the ...
interface problem is mitigated. The only detriment is that the code is enlarged a little.


C# example

public abstract class Saveable public class Customer : Saveable


See also

*
Template method pattern In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. in the book ''Design Patterns''. The template method is a method in a superclass, usually an abstract superclass, and defines ...


References

{{Reflist, 2


External links


Non-Virtual Interface
Software design patterns Articles with example Java code Method (computer programming)