When designing an interface, someone recommended to use the non-virtual interface pattern. Can someone briefly outline what the benefits of this pattern are?
Non-virtual interface design pattern in C#/C++
9.6k Views Asked by user705414 At
2
There are 2 best solutions below
0
ChrisWue
On
Here is a wiki article it a bit more in details with some examples. The essence is that you can ensure important conditions (like obtaining and releasing locks) in a central place in your base class while still allowing to derive from it to provide different implementations by using private or protected virtual functions.
Users of any class of the class hierarchy will always call the public interface which dispatches the calls to the not externally visible implementations.
Related Questions in C#
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in C++
- How to immediately apply DISPLAYCONFIG_SCALING display scaling mode with SetDisplayConfig and DISPLAYCONFIG_PATH_TARGET_INFO
- Why can't I use templates members in its specialization?
- How to fix "Access violation executing location" when using GLFW and GLAD
- Dynamic array of structures in C++/ cannot fill a dynamic array of doubles in structure from dynamic array of structures
- How do I apply the interface concept with the base-class in design?
- File refuses to compile std::erase() even if using -std=g++23
- How can I do a successful map when the number of elements to be mapped is not consistent in Thrust C++
- Can std::bit_cast be applied to an empty object?
- Unexpected inter-thread happens-before relationships from relaxed memory ordering
- How i can move element of dynamic vector in argument of function push_back for dynamic vector
- Brick Breaker Ball Bounce
- Thread-safe lock-free min where both operands can change c++
- Watchdog Timer Reset on ESP32 using Webservers
- How to solve compiler error: no matching function for call to 'dmhFS::dmhFS()' in my case?
- Conda CMAKE CXX Compiler error while compiling Pytorch
Related Questions in DESIGN-PATTERNS
- Will it slow down the performance when Unit of work pattern is used with EF Core
- Design patterns - How Design patterns work with bulk data
- Using Repository pattern to fetch data from different places and build list of objects
- Suggest best design patterns to update or insert bulk data
- Mapping one collection of objects into another collection of objects
- How can I break down a large presenter in the Viper design pattern into smaller pieces?
- How to create under the label in Textformfield, not a border, in Flutter
- Own Pattern / framework for interfacing with components in C
- Common Method Implementation for Elasticsearch and OpenSearch Java SDK
- How can I decouple them?
- Understanding Potential Deadlock in Resource Pool Implementation Described in "Go in Action"
- Dependency Injection Patterns stand alone implementaion
- How to use GoF design pattern for software robustness?
- Pipeline data processing and code architecture
- Mocking inherited class where new object is created or how to unsmell my class
Related Questions in NON-VIRTUAL-INTERFACE
- Mocking problem on Non-Virtual functions with NVI Idiom
- Make base NVI virtual function private?
- Does the Non-Virtual Interface idiom not apply to pure virtual functions?
- C# call an interface method non-virtual implementation
- NVI doesn't prevent name hiding; why not use virtual final instead?
- No type named 'type' in CTRP derived class
- Virtual template workaround for multi level NVI
- Is it possible to write a c++ class template that automatically wraps and virtualizes non virtual interfaces?
- Under the NVI idiom, why can't the virtual function be public?
- Bringing non-virtual interfaces and multi-level inheritance together
- Android activity life cycle: why isn't calling the super method first enforced?
- Adding invariants in non virtual interface idiom
- Clone method with a non virtual interface and some private variables
- Private inheritance and non-virtual interfaces
- Using the non-virtual-interface idiom, can/will my non-virtual function be inlined ?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
The essence of the non-virtual interface pattern is that you have private virtual functions, which are called by public non-virtual functions (the non-virtual interface).
The advantage of this is that the base class has more control over its behaviour than it would if derived classes were able to override any part of its interface. In other words, the base class (the interface) can provide more guarantees about the functionality it provides.
As a simple example, consider the good old animal class with a couple of typical derived classes:
This uses the usual public virtual interface that we're used to, but it has a couple of problems:
std::cout << ... << std::endl;boilerplate code.speak()does. A derived class may forget the new line, or write it tocerror anything for that matter.To fix this, you can use a non-virtual interface that is supplemented by a private virtual function that allows polymorphic behaviour:
Now the base class can guarantee that it will write out to
std::coutand end with a new line. It also makes maintenance easier as derived classes don't need to repeat that code.Herb Sutter wrote a good article on non-virtual interfaces that I would recommend checking out.