jeudi 27 février 2020

Factory Method without memory allocation C++

I have been searching for factory method without memory allocation and I have not found anything yet.

I want to make an inheritance tree like this:

Controller_Base_Class
   |                |
Specific_Class1     Specific_Class2 ...

In all factory methods that I've seen the helper class that creates the actual specific class, allocates memory with new and returns the object. For some reasons I would like to do it without new memory allocation.

Having that there won't be much Specific classes, would it make sense that the helper class had a reference of every Specific class and returns the one I need? Something like:

class BaseClass
{
   whatever
}

class Specific1 : public BaseClass
{
   whatever
}

class Specific2 : public BaseClass
{
   whatever
}

class Factory
{
    public:
        Factory();
        BaseClass* Create(SpecType type);
    private:
        Specific1 specific1;
        Specific2 specific2;
}
Factory::Factory() : specific1(), specific2()
{
}
BaseClass* Factory::Create(SpecType type)
{
    if(type==SpecType1)
    {
        return &specific1;
    }
    ...
}

...
//function where i want to do it
Factory factory = Factory()
BaseClass* inheritance_holder = factory.Create(Spec1Type);
...

Aucun commentaire:

Enregistrer un commentaire