【发布时间】:2013-01-07 07:23:36
【问题描述】:
我知道策略和抽象工厂设计模式 - 但是它们并不能解决我当前的问题:
我正在创建一个提供非常基本的 GUI 的 C++ 库。但是我希望用户能够在编译时选择使用哪个 GUI 库(比如 Qt 或 FLTK)来实际呈现 GUI。然而,用户应该只需要了解我的库中的方法。
应该可以使用 Qt 后端或 FLTK 后端编译相同的代码而无需任何更改。
我想到了类似的东西:
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
}
问题是我不希望用户知道QT_A 或FLTK_A。用户(开发人员)应该只处理A。此外,我不能同时拥有这两种变体,因为我不希望我的库同时依赖于 Qt 和 FLTK;只是在编译时选择的那个。
【问题讨论】:
-
首先想到的就是使用预处理器……希望你能得到更好的答案。
-
不叫QT,叫Qt(QT = QuickTime, Qt = cute ;))。
标签: c++ design-patterns inheritance cmake