Return to Snippet

Revision: 68642
at February 2, 2015 13:48 by dongwonkwak


Initial Code
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_;
};

Initial URL

                                

Initial Description
template factory in modern c++ design

Initial Title
Template Factory

Initial Tags
design, c++

Initial Language
C++