在工厂模式的例子中,静态成员函数static factory()迫使所有创建对象的操作都集中在一个地方,因此这个地方就是唯一需要修改代码的地方。然而,GoF强调工厂方法模式的理由是,可以使不同类型的工厂派生自基本类型的工厂。工厂方法模式事实上是多态工厂模式的一个特例。
#include <iostream> #include <map> #include <string> #include <vector> #include <stdexcept> #include <cstddef> //#include "../purge.h" using namespace std; class Shape { public: virtual void draw() = 0; virtual void erase() = 0; virtual ~Shape() {} }; // 具体Shape创建工厂类的基类 class ShapeFactory{ // 私有的,虽不能直接调用,但可以被覆盖,由具体的Shape实现 virtual Shape* create() = 0; // 用于记录类的名称和该类的创建工厂 static map<string, ShapeFactory*> factories; public: virtual ~ShapeFactory() {} friend class ShapeFactoryInitializer; class BadShapeCreation : public logic_error { public: BadShapeCreation(string type) : logic_error("Cannot create type " + type) {} }; // 通过查找map表factories中目标类的创建工厂, //调用该创建工厂的create()方法创建目标类的对象 static Shape* createShape(const string& id) throw(BadShapeCreation) { if(factories.find(id) != factories.end()) return factories[id]->create(); else throw BadShapeCreation(id); } }; // Define the static object: map<string, ShapeFactory*> ShapeFactory::factories; class Circle : public Shape { Circle() {} // Private constructor friend class ShapeFactoryInitializer; // 当前类的创建工厂,实际上就是为了封装create方法,即“方法对象” class Factory; friend class Factory; class Factory : public ShapeFactory { public: // 实现当前对象的创建 Shape* create() { return new Circle; } // 这一句似乎没多大意义,除非将将Facotry()设为私有 friend class ShapeFactoryInitializer; }; public: void draw() { cout << "Circle::draw" << endl; } void erase() { cout << "Circle::erase" << endl; } ~Circle() { cout << "Circle::~Circle" << endl; } }; class Square : public Shape { Square() {} // Private constructor friend class ShapeFactoryInitializer; class Factory; friend class Factory; class Factory : public ShapeFactory { public: Shape* create() { return new Square; } friend class ShapeFactoryInitializer; }; public: void draw() { cout << "Square::draw" << endl; } void erase() { cout << "Square::erase" << endl; } ~Square() { cout << "Square::~Square" << endl; } }; // Singleton to initialize the ShapeFactory: class ShapeFactoryInitializer { static ShapeFactoryInitializer si; ShapeFactoryInitializer() { ShapeFactory::factories["Circle"]= new Circle::Factory; ShapeFactory::factories["Square"]= new Square::Factory; } ~ShapeFactoryInitializer() { map<string, ShapeFactory*>::iterator it = ShapeFactory::factories.begin(); while(it != ShapeFactory::factories.end()) delete it++->second; } }; // Static member definition: ShapeFactoryInitializer ShapeFactoryInitializer::si; char* sl[] = { "Circle", "Square", "Square", "Circle", "Circle", "Circle", "Square" }; int main() { vector<Shape*> shapes; try { for(size_t 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 EXIT_FAILURE; } for(size_t i = 0; i < shapes.size(); i++) { shapes[i]->draw(); shapes[i]->erase(); } //purge(shapes); }
这时,每个类都用自己的创建工厂Factory创建自己类型的对象,所有的创建工厂都继承自同一个基类ShapeFactory。
当增加一个新类型到这个设计时,必须定义该类型,创建一个工厂并修改ShapeFactoryInitializer,以便将工厂的一个实例插入map中。
选自《C++编程思想》。
原文链接: https://www.cnblogs.com/chinaxmly/archive/2012/10/01/2709862.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/64375
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!