Dynamisation of Static Polymorphism - GerdHirsch/Cpp-TemplateBasics GitHub Wiki

With a simple adapter we can easily dynamise template-based Static Polymorphism design without changing the existing classes.
Only the application looks slightly different.

You need an abstract base class like in Dynamic Polymorphism Design
that defines the interface expected by the component as virtual functions
and an adapter template that inherits from this base class.
The template parameter of the adapter in turn represents the abstraction of the variants.
The adapter delegates the messages of the component to these.

The component is instantiated with the abstract base class during application.
This is ensured by overloading the createComponent function with an adapter pointer.
Each adapter and thus any variant can be exchanged at runtime.

The interesting thing about this design is
that all components are completely independent of each other.
The overhead is no greater than with "normal" dynamic polymorphism
because only the operations of the adapter are virtual
and the methods of the implementations can be inlined.

One might be inclined to use this design in general.
This is also an example of how more properties can be added to a class non-invasively.

DynamisedStaticPolymorphism

see the C++ Implementation of the Example
and the usage

previous Static Polymorphism