/ Published in: C++
template factory in modern c++ design
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
template < class AbstractProduct, typename IdentifierType, typename ProductCreator = std::function<AbstractProduct* ( void)> > class Factory { public: bool Register(const IdentifierType& id, ProductCreator creator) { associations_.insert( AssocMap::value_type(id, creator)); return true ; } AbstractProduct* CreateObject( const IdentifierType& id) { typename AssocMap::const_iterator i = associations_.find(id); if (i != associations_.end()) { return ((*i).second)(); } return NULL; } private: typedef map<IdentifierType, ProductCreator> AssocMap; AssocMap associations_; };