/* ****************************************************************** * * Marcos Abreu Bento * * marcosbento@gmail.com * * ------------------------------------------------------------------ * * My implementation of Factory Method design pattern * * ****************************************************************** */ #ifndef FACTORY_HPP_ #define FACTORY_HPP_ #include #include template // P is for factory Product class Factory { private: /* Generation of products is based on calling a specific function. */ typedef P*(*Generator)(void); std::map products; public: void registerProduct(const std::string& key, const Generator& generator); P *newProduct(const std::string& key); /* Derived Products (DP) are created by default construction. * Generation depends of Base and Derived types, * and can be specified outside he factory. */ template static P* generate(); }; template void Factory

::registerProduct(const std::string& key, const Generator& generator) { /* Generator is assigned at registration time */ products[key] = generator; } template P* Factory

::newProduct(const std::string& key) { return products[key](); } template template P* Factory

::generate() { return new DP(); } #endif