我们可以从面包房购买到提拉米苏、拿破仑,当他们新出一款抹茶蛋糕时,收银台是不会被拆掉的,还是需要花钱购买
工厂模式就是为我们提供了创建对象的统一接口,新增的类型不会影响到接口
工厂模式包含三种:静态工厂、多态工厂、抽象工厂,它属于构造型设计模式
- 静态工厂使用简单,适合于变化较少的情况
namespace STATIC_FACTORY {
class Shape {
public:
class BadCreation : public std::logic_error {
public:
BadCreation(const string& type)
: std::logic_error("Can not create " + type)
{
}
};
static Shape* createFactory(const string& type) throw(BadCreation);
virtual void draw() = 0;
virtual void erase() = 0;
virtual ~Shape() {};
};
class Circle : public Shape {
Circle() {}
friend class Shape;
public:
void draw() { cout << "Circle::draw()" << endl; }
void erase() { cout << "Circle::erase()" << endl; }
};
class Square : public Shape {
Square() {}
friend class Shape;
public:
void draw() { cout << "Square::draw()" << endl; }
void erase() { cout << "Square::erase()" << endl; }
};
Shape* Shape::createFactory(const string& type)
throw(Shape::BadCreation)
{
if ("Circle" == type) {
return new Circle();
}
else if ("Square" == type) {
return new Square();
}
else {
throw BadCreation(type);
return NULL;
}
}
void staticFactoryTest()
{
const string types[] = {"Circle", "Square", "abc", "Circle"};
vector<Shape*> shapes;
try {
for (int i = 0; i < sizeof(types)/sizeof(types[0]); i++)
{
shapes.push_back(Shape::createFactory(types[i]));
}
} catch (Shape::BadCreation e) {
cout << e.what() << endl;
return;
}
for (int i = 0; i < shapes.size(); i++)
{
shapes[i]->draw();
shapes[i]->erase();
}
}
}
2.多态工厂是静态工厂的升级版,是将factory抽象出来,可以更容易的增加构建的类型,但同时增加了开销
class Shape {
public:
virtual void draw() = 0;
virtual void erase() = 0;
virtual ~Shape() {}
};
class ShapeFacotryInitializer;
class ShapeFactory {
virtual Shape *create() = 0;
static map<string, ShapeFactory*> factories;
public:
virtual ~ShapeFactory() {}
friend class ShapeFacotryInitializer;
class BadShapeCreation : public logic_error {
public:
BadShapeCreation(string type)
: logic_error("Cannot create type " + type) {}
};
static Shape *createShape(const string& id) throw(BadShapeCreation) {
if (factories.find(id) != factories.end())
return factories[id]->create();
else
throw BadShapeCreation(id);
}
};
map<string, ShapeFactory*> ShapeFactory::factories;
class Circle : public Shape {
Circle() {}
friend class ShapeFacotryInitializer;
class Factory;
friend class Factory;
class Factory : public ShapeFactory {
public:
Shape *create() { return new Circle(); }
};
public:
void draw() { cout << "Circle::draw" << endl; }
void erase() { cout << "Circle::erase" << endl; }
~Circle() { cout << "Circle::~Circle" << endl; }
};
class Square : public Shape {
Square() {}
friend class ShapeFacotryInitializer;
class Factory;
friend class Factory;
class Factory : public ShapeFactory {
public:
Shape *create() { return new Square(); }
};
public:
void draw() { cout << "Square::draw" << endl; }
void erase() { cout << "Square::erase" << endl; }
~Square() { cout << "Square::~Square" << endl; }
};
class ShapeFacotryInitializer {
static ShapeFacotryInitializer si;
ShapeFacotryInitializer() {
ShapeFactory::factories["Circle"] = new Circle::Factory();
ShapeFactory::factories["Square"] = new Square::Factory();
}
~ShapeFacotryInitializer() {
map<string, ShapeFactory*>::iterator it = ShapeFactory::factories.begin();
while (it != ShapeFactory::factories.end())
delete it++->second;
}
};
ShapeFacotryInitializer ShapeFacotryInitializer::si;
char *sl[] = {"Circle", "Square", "Circle"};
int FactoryTest() {
vector<Shape*> shapes;
try {
for (int i = 0; i < sizeof(sl)/sizeof(sl[0]); i++)
shapes.push_back(ShapeFactory::createShape(sl[i]));
} catch(ShapeFactory::BadShapeCreation e) {
cout << e.what() << endl;
return -1;
}
for (int i = 0; i < shapes.size(); i++) {
shapes[i]->draw();
shapes[i]->erase();
}
}
3.抽象工厂是工厂模式的一个集合,是工厂模式与策略模式的结合,提高了构建对象的灵活度