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

若如初见. 提交于 2019-12-02 08:53:56

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!