Rahul Sharma (Editor)

Non virtual interface pattern

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

The non-virtual interface pattern (NVI) controls how methods in a base class 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. 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:

  1. Client interface: This is the public non-virtual interface
  2. 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 interface problem is mitigated. The only detriment is that the code is enlarged a little.

References

Non-virtual interface pattern Wikipedia