A C++ Object Factory (2024)

The Problem

In object oriented programming, it is very common to have a scenario where a largenumber of classes share the same base class and the particular implementation needsto be created at runtime based on some specific parameters, for example a class nameheld in a string.

Standard C++ does not provide the same type of reflection mechanism that other languagesuse to achieve this, such as C# :

C++

System.Reflection.Assembly.GetExecutingAssembly() .CreateInstance(string className)

or in Java:

C++

Class.forName(className).getConstructor(String.class).newInstance(arg);

However, C++ does not allow us to do such things; we have to come up with another solution.The basic pattern, or set of patterns, that help us achieve this are factory patterns.

A Simple Solution

The Base Class

Our base class is defined as an abstract class as follows:

C++

#ifndef CPPFACTORY_MYBASECLASS_H#define CPPFACTORY_MYBASECLASS_Hclass MyBaseClass{public: virtual void doSomething() = 0;};#endif // CPPFACTORY_MYBASECLASS_H

The Factory Class

A factory method can then be defined as a static method that can be used to createinstances of MyBaseClass. We could define this as a static method on MyBaseClassitself, although it is generally good practice in object oriented development that a classserves a single purpose. Therefore, lets create a factory class:

C++

#ifndef CPPFACTORY_MYFACTORY_H#define CPPFACTORY_MYFACTORY_H#include "MyBaseClass.h"#include <memory>#include <string>using namespace std;class MyFactory{public: static shared_ptr<MyBaseClass> CreateInstance(string name);};#endif // CPPFACTORY_MYFACTORY_H

The factory method is expected to create an instance of a class named name that is derivedfrom MyBaseClass and return it as a shared pointer, as it will relinquish ownershipof the object to the caller.

We shall return to the implementation of the method shortly.

Some Derived Classes

So lets implement a couple of derived classes:

#ifndef CPPFACTORY_DERIVEDCLASSONE_H#define CPPFACTORY_DERIVEDCLASSONE_H#include "MyBaseClass.h"#include <iostream>using namespace std;class DerivedClassOne : public MyBaseClass{public: DerivedClassOne(){}; virtual ~DerivedClassOne(){}; virtual void doSomething() { cout << "I am class one" << endl; }};#endif // CPPFACTORY_DERIVEDCLASSONE_H

and

C++

#ifndef CPPFACTORY_DERIVEDCLASSTWO_H#define CPPFACTORY_DERIVEDCLASSTWO_H#include "MyBaseClass.h"#include <iostream>using namespace std;class DerivedClassTwo : public MyBaseClass{public: DerivedClassTwo(){}; virtual ~DerivedClassTwo(){}; virtual void doSomething() { cout << "I am class two" << endl; }};#endif // CPPFACTORY_DERIVEDCLASSTWO_H

A First Attempt at the Factory Method

A simple solution to the implementation of the factory method would be something likethis:

C++

#include "MyFactorySimple.h"#include "DerivedClassOne.h"#include "DerivedClassTwo.h"shared_ptr<MyBaseClass> MyFactory::CreateInstance(string name){ MyBaseClass * instance = nullptr; if(name == "one") instance = new DerivedClassOne(); if(name == "two") instance = new DerivedClassTwo(); if(instance != nullptr) return std::shared_ptr<MyBaseClass>(instance); else return nullptr;}

The factory determines which concrete class to create and has knowledge of every classvia the class headers.

Running the application

A simple main function is now needed so that we can test our implementation:

C++

#include "MyFactorySimple.h"int main(int argc, char** argv){ auto instanceOne = MyFactory::CreateInstance("one"); auto instanceTwo = MyFactory::CreateInstance("two"); instanceOne->doSomething(); instanceTwo->doSomething(); return 0;}

A Visual Studio Project (SimpleFactory.vcxproj) is included with the source code accompanyingthis article which can be built and run giving the following output:

C++

I am class oneI am class two

Problems with the Simple Solution

On the surface this looks like a good solution and it possibly is in some cases. However,what happens if we have a lot of classes deriving from MyBaseClass? We keep havingto add the includes and the compare – construct code.

The problem now is that the factory has an explicit dependency on all the derivedclasses, which is not ideal. We need to come up with a better solution; one that removesthe need for constantly adding to the MyFactory::Create. This is where the idea of aregistry of factory methods can help us.

A Revised Factory Class

One of our main objectives is to remove the dependencies on the derived classes fromthe factory. However, we still need to allow the factory to trigger the creation of instances.One way to do this is for the main factory class to maintain a registry of factoryfunctions that can be defined elsewhere. When the factory class needs to create an instanceof a derived class, it can look up the factory function in this registry. The registryis defined as follows:

C++

map<string, function<MyBaseClass*(void)>> factoryFunctionRegistry;

It is a map, keyed on a string with values as functions that return a pointer to an instanceof a class based on MyBaseClass.

We can then have a method on MyFactory which can add a factory function to the registry:

C++

void MyFactory::RegisterFactoryFunction(string name,function<MyBaseClass*(void)> classFactoryFunction){ // register the class factory function factoryFunctionRegistry[name] = classFactoryFunction;}

The Create method can then be changed as follows:

C++

shared_ptr<MyBaseClass> MyFactory::Create(string name){ MyBaseClass * instance = nullptr; // find name in the registry and call factory method. auto it = factoryFunctionRegistry.find(name); if(it != factoryFunctionRegistry.end()) instance = it->second(); // wrap instance in a shared ptr and return if(instance != nullptr) return std::shared_ptr<MyBaseClass>(instance); else return nullptr;}

So how do we go about registering the classes in a way that keeps dependencies to aminimum? We cannot easily have instances of the derived classes register themselvesas we can’t create instances without the class being registered. The fact that we need theclass registered, not the object gives us a hint that we may need some static variablesor members to do this.

I stress that the way I am going to do this may not be the best in all scenarios. I am deeplysuspicious of static variables and members, as static initialization can be a minefield.However, I will press on, as the solution serves the purpose of this example and it is upto the reader to determine whether a solution they use needs to follow different rulesand design.

Firstly we define a method on MyFactory to obtain the singleton instance:

C++

MyFactory * MyFactory::Instance(){ static MyFactory factory; return &factory;}

We cannot call the following from the global context:

C++

MyFactory::Instance()->RegisterFactoryFunction(name, classFactoryFunction);

I have therefore created a Registrar class that will do the call for us in its constructor:

C++

class Registrar {public: Registrar(string className, function<MyBaseClass*(void)> classFactoryFunction);};...Registrar::Registrar(string name, function<MyBaseClass*(void)> classFactoryFunction){ // register the class factory function  MyFactory::Instance()->RegisterFactoryFunction(name, classFactoryFunction);}

Once we have this, we can create static instances of this in the source files of the derivedclasses as follows (DerivedClassOne):

C++

static Registrar registrar("one",[](void) -> MyBaseClass * { return new DervedClassOne();});

As it turns out, this code can be duplicated in all derived classes so a quick pre processordefine as follows:

C++

#define REGISTER_CLASS(NAME, TYPE) \ static Registrar registrar(NAME, \ [](void) -> MyBaseClass * { return new TYPE();});

This uses the new C++ lambda support to declare anonymous functions. We thenonly need add the following to each derived class source file:

C++

REGISTER_CLASS("one", DerivedClassOne);

Update 25th January 2013

We Can Do Better …

Although the #define solution provides a neat implementation we could probably do this in a bit more of a C++ style by converting the Registrar class into a template class as follows:

C++

template<class T>class Registrar {public: Registrar(string className) { // register the class factory function  MyFactory::Instance()->RegisterFactoryFunction(name, [](void) -> MyBaseClass * { return new T();}); }};

And now we can replace the use of the macro by

C++

static Registrar<DerivedClassOne> registrar("one");

We now have a function registry based factory class defined and the main function cannow be slightly modified as follows:

C++

#include "MyFactory.h"int main(int argc, char** argv){ auto instanceOne = MyFactory::Instance()->Create("one"); auto instanceTwo = MyFactory::Instance()->Create("two"); instanceOne->doSomething(); instanceTwo->doSomething(); return 0;}

We can now build and run the project and get the following output:

I am class oneI am class two

References:

A C++ Object Factory (2024)
Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5952

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.