Search code examples
c++classtypedeffactory-patterninstances

Factory Pattern: typedef Class *(createClassFunction)(void)


What does typedef Class *(createClassFunction)(void) (or another variation is typedef Class* (__stdcall *CreateClassFunction)(void))stand for? What does it mean? How am I supposed to explain it? Especially in the context of Factory Patterns...


Solution

  • Reading C type expressions

    createClassFunction is a typedef for a function taking no arguments and returning a Class *.

    With that declaration, a pointer to such a funciton can obviously act as factory for a Class. Usage might be as follows:

    // the class factory
    Class * MostTrivialFactoryKnownToMan()
    {
     return new Class;
    }
    
    // another class factory
    Class * CreateClassWithLog()
    {
       ClassWithLog * p = new ClassWithLog; // derived from Class
       p->SetLogFile("log.txt");
       return p;
    }
    
    // code consuming the class factory
    void PopulateStars(createClassFunction * factory)
    {
       // creates many instances of `Class` through `factory`
       Sky * sky = GetSky();
       for(int i=0; i<sky->GetStarCount(); ++i)
       {
          Class * inhabitant = (*factory)();
          sky->GetStar(i)->SetInhabitant(inhabitant);
       }
    }
    
    // code deciding which factory to use
    const bool todayWeWriteALog = (rand() %2) != 0;
    createClassFunction * todaysFactory = todayWeWriteALog ?
       &MostTrivialFactoryKnownToMan : 
       &CreateClassWithLog);
    
    PopulateStars(factory);
    

    __stdcallis a compiler specific attribute changin the calling convention (how parameters and return value are passed between caller and implementation). This is often important for binary compatibility - e.g. when a Pascal program needs to call a funciton imlemented in C or C++.

    Issues:

    The factory function returns a raw pointer. There must be an implicit contract between the factory and the consumer how to free that pointer (e.g. through delete in or example). Using a smart pointer (e.g. shared_ptr) for the return type would allow the factory to determine the deletion policy.

    The factory, as a function pointer, may not hold state (such as the log file name, it needs to be hard coded in the function, or available globally). using a callable object instead would allow to implement configurable factories.