#include <iostream>
class Shape
{
public:
virtual void drawshape() = 0;
};
class DrawCircle : public Shape
{
public:
void drawshape() {
std::cout << "Circle\n";
}
};
class DrawRect : public Shape
{
public:
void drawshape() {
std::cout << "Rect\n";
}
};
class DrawShape
{
public:
Shape* getshape(std::string shape) {
if ("Circle" == shape) {
return new DrawCircle();
}
if ("Rect" == shape) {
return new DrawCircle();
}
return NULL;
}
};
int main()
{
DrawShape shape;
Shape* p_shape = shape.getshape("Circle");
if (!p_shape) {
std::cout << "Get shape failed.\n";
return -1;
}
p_shape->drawshape();
return 0;
}
C++ 实现设计模式 -- 工厂模式
最新推荐文章于 2022-07-29 21:28:07 发布