mercredi 4 mai 2016

Object Factory in C++ using map

I want to create an object Factory, and I want to implement it using a map [string,"pointer to function"]. The thing is, the objects I will be creating derive from the same class, but their constructors take different parameters. Those "pointer to function" would point to a member function of this Factory which returns a new object, calling the constructor with the appropriate parameters.

class A{
    //...
};

class B{
    //...
};

class Handler{
    // ...
};

class HandlerA:public Handler{
    public:
        HandlerA(A myA){
            //...
        }
};

class HandlerB:public Handler{
    public:
        HandlerB(B myB){
            //...
        }
};


class HandlerSelector{
    private:
        A myA;
        B myB;
        std::map<std::string,XXX pointer> factory;
    public:

        Handler* myHandlerACreator(){
            return new HandlerA(myA);
        }
        Handler* myHandlerBCreator(){
            return new HandlerB(myB);
        }
        HandlerSelector(){
            factory["A"]=//pointer to myHandlerACreator
            factory["B"]=//pointer to myHandlerBCreator
        }

        Handler* getHandler(std::string type){
            return factory[type]// use pointer to myHandlerXCreator to return new HandlerA or HandlerB
        }

};

So, how should I declare/fill the map and use those pointers?

Aucun commentaire:

Enregistrer un commentaire