samedi 28 mars 2015

Generalized abstarct factory


In Modern C++ Design, Alexandrescu describes disadvateages of an object factory, designed as follows:



class WidgetFactory
{
public:
virtual Window* CreateWindow() = 0;
virtual Button* CreateButton() = 0;
virtual ScrollBar* CreateScrollBar() = 0;
};


One of those disadvanteges is unable to create a widget in a Generic way:



template <class T>
T* MakeRedWidget(WidgetFactory& factory)
{
T* pW = factory.CreateT(); // huh???
pW->SetColor(RED);
return pW;
}


or



template <class T>
T* MakeRedWidget(WidgetFactory& factory)
{
T* pW = factory.Create<T>(); // aha!
pW->SetColor(RED);
return pW;
}


if a creation function were a function template. So, the solution he proposed was a TypeList. OK, we have typlists concept now, but it's still not clear how we can create a generic object factory solving the problem about generic widget creation.



template <class TList>
class WidgetFactory
{
//What??
}



Aucun commentaire:

Enregistrer un commentaire