寻找一种设计模式以应用于C++
Looking for a design pattern to apply in C++
我有这些C++类定义如下:
class A
{
public:
B *createB();
};
class B
{
public:
virtual void fun() = 0;
};
class B1 : B {/* ... */};
class B2 : B {/* ... */};
所以基本上 B 是一个抽象类,B1 和 B2 是 B 的具体实现,A 在其代码中的某个地方创建了一个类型 B 的实例。需要注意的是,A 不是工厂,A::createB
只是一个例子。
我希望能够在 A 初始化期间传递 B 的子类,以便可以在运行时根据需要由后者创建前者的实例。例:
A *a1 = /* sorcery here */;
A *a2 = /* another magic code */;
a1->createB(); // getting B1
a2->createB(); // getting B2
实现它的最佳方法是什么?是否可以不使用模板?
<小时 />根据回应,我最终得到了这个。谢谢!
class B
{
public:
virtual void fun() = 0;
virtual B *clone() = 0;
};
class B1 : public B
{
public:
virtual void fun()
{
std::cout << "B1" << std::endl;
}
virtual B *clone()
{
return new B1();
}
};
class B2 : public B {/* analogous to B1 */};
class A
{
public:
A(B *b) : b(b) {};
B *createB()
{
return b->clone();
}
private:
B *b;
};
A(new B1()).createB()->fun(); // prints "B1"
A(new B2()).createB()->fun(); // prints "B2"
在 B 中实现一个clone()
方法。
创建 A 时将B*
传递给 A。A 将使用该B*
作为参数调用 B 的clone()
。
有关克隆的更多信息,请参阅问题 哪种情况将在C++中使用克隆以及如何使用它?、C++中克隆() 的最佳签名是什么?以及如何轻松编写克隆方法?等。
您可以使用原型设计模式来实现此目的。A
传递 B1
或 B2
的实例,并向B
添加一个 clone()
成员函数,如下所示:
class B
{
public:
virtual void fun() = 0;
virtual B* clone() = 0; // B1::clone returns new B1; B2::clone returns new B2
};
A
存储初始化期间传入的B
的原型实例供以后使用。当它稍后需要创建新B
时,它会调用原型上的clone()
以获取正确类的实例。
听起来你想要抽象工厂。
你可以在这里看到例子:
#include <iostream>
#include <string>
class Window
{
protected:
int width;
int height;
std::string toolkit;
std::string type;
Window(std::string usedToolkit, std::string windowType)
: toolkit(usedToolkit), type(windowType)
{}
public:
std::string getToolkit()
{
return toolkit;
}
std::string getType()
{
return type;
}
};
class GtkToolboxWindow : public Window
{
public:
GtkToolboxWindow()
: Window("Gtk", "ToolboxWindow")
{}
};
class GtkLayersWindow : public Window
{
public:
GtkLayersWindow()
: Window("Gtk", "LayersWindow")
{}
};
class GtkMainWindow : public Window
{
public:
GtkMainWindow()
: Window("Gtk", "MainWindow")
{}
};
class QtToolboxWindow : public Window
{
public:
QtToolboxWindow()
: Window("Qt", "ToolboxWindow")
{}
};
class QtLayersWindow : public Window
{
public:
QtLayersWindow()
: Window("Qt", "LayersWindow")
{}
};
class QtMainWindow : public Window
{
public:
QtMainWindow()
: Window("Qt", "MainWindow")
{}
};
/* This is the abstract factory. */
class UIFactory
{
public:
virtual Window* getToolboxWindow() = 0;
virtual Window* getLayersWindow() = 0;
virtual Window* getMainWindow() = 0;
};
/* Factory for Gtk toolkit */
class GtkUIFactory : public UIFactory
{
public:
Window* getToolboxWindow()
{
return new GtkToolboxWindow();
}
Window* getLayersWindow()
{
return new GtkLayersWindow();
}
Window* getMainWindow()
{
return new GtkMainWindow();
}
};
/* Factory for Qt toolkit */
class QtUIFactory : public UIFactory
{
public:
Window* getToolboxWindow()
{
return new QtToolboxWindow();
}
Window* getLayersWindow()
{
return new QtLayersWindow();
}
Window* getMainWindow()
{
return new QtMainWindow();
}
};
int main()
{
UIFactory* ui = 0;
/* Check what environment is running
and create appropriate factory. */
if (/* Gtk == */ true)
{
ui = new GtkUIFactory();
}
else
{
ui = new QtUIFactory();
}
/* Use the factory to build interface. */
Window* toolbox = ui->getToolboxWindow();
Window* layers = ui->getLayersWindow();
Window* main = ui->getMainWindow();
/* See what have we recieved. */
std::cout << toolbox->getToolkit() << ":"
<< toolbox->getType() << std::endl;
std::cout << layers->getToolkit() << ":"
<< layers->getType() << std::endl;
std::cout << main->getToolkit() << ":"
<< main->getType() << std::endl;
}
相关文章:
- 用于在回调中调用解析器的设计模式
- 在任何编译语言中实现以下语法(用于工厂设计模式)? 最好是 Kotlin,C++
- 用于存储由第一个功能创建的指针的设计模式将在以后使用
- 用于对基于I/O的对象执行一系列操作的设计模式
- 用于在硬件接口之间切换的最佳设计模式
- GUI软件设计模式,用于在运行时管理多个小部件
- 寻找一种设计模式以应用于C++
- 用于在不使用 RTTI 的情况下缓存不同派生类型的设计模式
- 是否有推荐的设计模式用于与过程通信参数
- 用于独立访问器/赋值器接口的C++设计模式
- 用于通过常量中介访问非常量的设计模式
- 用于MFC应用程序的任何可测试的架构或设计模式
- 设计模式,用于创建对向量中元素的智能引用
- 用于创建具有const或非const属性的对象的设计模式
- 什么OOD设计模式应该用于服务器-处理-客户端请求
- 是否存在用于在子类的数据上实施规则的设计模式
- 更好的设计模式用于读取其他进程内存
- 用于分配器类型参数的C++设计模式
- C++设计模式,用于在另一个类中使用抽象类对象
- 如何将MVC模式应用于GUI开发