Breaking News
Loading...
Tuesday 10 April 2007

Virtuality is an implementation detail !

13:06
There are many ways of achieving separation of interface from implementation:

1. Using Interface Definition Language (IDL) as in CORBA
2. Bridge Pattern (GoF)
3. Pimpl idiom
4. Handle/Body idiom
5. Envelope/Letter idiom
6. Template Method pattern (GoF)

The Template Method way of achieving this is a bit tricky. It basically boils down to a few class design guidelines based on access modifiers, virtuality and language expressibility. It is also known as Non-Virtual Interface (NVI) idiom.

1. Prefer to make interfaces non-virtual, using Template Method.
2. Prefer to make virtual functions private.
3. Only if derived classes need to invoke the base implementation of a virtual function, make the virtual function protected.
4. A base class destructor should be either public and virtual, or protected and non-virtual.

For more information please see: Virtuality and Virtually Yours.
A few more elemental base class idioms can be found here.
A comparison of the above approaches can be found in this ACCU article.

Thinking of it more, the Template Method used in this way can be thought of as a special case of Bridge, where the pointer to implementation is implicitely replaced by this pointer.

0 comments:

Post a Comment

 
Toggle Footer