I know about the Strategy and Abstract Factory design patterns - however they don't solve my current problem:
I'm creating a C++ library that offers a very basic GUI. However I want the user to be able to choose at compile time which GUI library to use (say Qt or FLTK) to actually render the GUI. The user should however only need to know about the methods in my library.
It should be possible to compile the same code without any changes using either a Qt backend or an FLTK backend.
I thought of something like:
class A
{
// do things that are not specific to QT or FLTK here as there are many
// methods I will need independent of the backend
}
class QT_A : public A
{
// Implement the actual creation of a window, display of a widget here using Qt
}
class FLTK_A : public A
{
// Implement the actual creation of a window, display of a widget here using FLTK
}
The problem is that I do not want the user to know about QT_A
or FLTK_A
. The user (developer) should just deal with A
. Also, I can't have both variants at the same time as I don't want my library to depend on both Qt and FLTK; just whichever was chosen at compile time.
The Pimpl idiom may be an alternative. It allows you to create a common interface without framework dependent members.
Then, the source file can provide different implementations of impl depending on the backend.
One option is the Pimpl idiom described in another answer.
Another option is a factory returning a pointer to the interface class:
No need of fancy patterns.
You distribute
A
,QT_A
, andmake_A
function;A
,FLTK_A
and another implementation ofmake_A
function.The user links to either library.