HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Generic C++ Factory

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
factorygenericstackoverflow

Problem

For my project, I needed a way to instantiate different object types during runtime using "string names", for this I designed a generic factory that is created for each object hierarchy type (currently there are two different hierarchies that need the factory).

We want the factory to be simple to use and simple to register new types, for this, the factory relies on two classes types.

The first one, is the object creator, that each new class that needs to register on the factory instantiates, this is constructed as shown below:

template 
class ObjectCreatorBase_c: public ObjectCreatorAutoUnlinkHook_t
{
    public:
        typedef T ObjectType_t;
        typedef Y ObjectCreatorProc_t;

     public:
        ObjectCreatorBase_c(const String_c &name, ObjectCreatorProc_t proc):
            strName(name),
            pfnCreateProc(proc)
        {
            if(proc == NULL)
            {
                std::stringstream stream;
                stream << "creator proc cant be null, entity " << name;
                PH_RAISE(INVALID_PARAMETER_EXCEPTION, "[ObjectCreatorBase_c::ObjectCreatorBase_c]", stream.str());
            }
        }

        T Create(const String_c &name) const
        {
            return pfnCreateProc(name);
        }

        inline const String_c &GetName() const
        {
            return strName;
        }

        inline bool operator<(const ObjectCreatorBase_c &rhs) const
        {
            return strName.compare(rhs.strName) < 0;
        }

    private:
        String_c strName;

    protected:
        ObjectCreatorProc_t pfnCreateProc;
};


This base class is created because there are two types (right now) of creation function, one with a single parameter for the constructor and another with two. Each type is specialized as shown below:

```
template
class ObjectCreator_c: public ObjectCreatorBase_c
{
public:
typedef ObjectCreatorBase_c BaseType_t;

public:
ObjectCreator_c(const Str

Solution

Here are two things that you could do to generalise the Factory pattern and decouple the parameter problem. Either or both would make your life easier.

  • You could consider using the concept of boost::any to allow an arbitrary number arguments of any type in your constructor list.



  • Write in the concept of the Null type (an empty class) to represent the case where the parametrised type Y is empty.



  • Traits classes allow object hierarchies to do behaviour selection based on type.



Your Generic Factory is a creation behaviour that depends on type. A traits class for each of your creatable object types that is able to allow both the construction and interpretation of the array of any type that the factory interface understands will allow you to decouple the Factory from the Type and the Type should always have both a default constructor and possibly custom constructor(s) that the traits class knows how to use.

PS: I am not a fan of this class type naming style ObjectType_t. The type name is a double tautology.

Context

StackExchange Code Review Q#3248, answer score: 3

Revisions (0)

No revisions yet.