c++design-patternsabstract-factorystatic-polymorphismc++-loki

why AbstractFactoryUnit has dynamic instead of static polymorphism?


I'm in a process of learning modern c++ and focusing on abstract factory at the moment, and from what I understand, one of the main ideas for Loki is to avoid "virtual" (dynamics polymorphism) that is used in GoF. But sometimes, it is still used. In a chapter on abstract factory, AbstractFactoryUnit has "virtual". http://loki-lib.cvs.sourceforge.net/loki-lib/loki/include/loki/AbstractFactory.h?view=markup from what i read so far, there are ways to go around it. http://en.wikipedia.org/wiki/Template_metaprogramming#Static_polymorphism

why is it more effective to use virtual in this case?


Solution

  • Since Loki's goals seem mainly educational, I'd say static polymorphism might have been avoided just for clarity's sake. When teaching a design pattern like abstract factory, the relationships between classes are the most important aspect. How polymorphism is implemented seems like a secondary, implementation-related issue in this context.

    True, static polymorphism might be more efficient, but it decreases code readability.

    You might argue that if you were to create big objects frequently, you'd benefit from using static polymorphism. And that's very true, but I think Loki is not meant to be used in such a scenario. Its main purpose is teaching.

    Take a look at the source code of Boost.Factory, that might help. In fact, I'd rather study modern C++ from Boost source code; true, Loki is said to have laid the foundations for Boost, but keep in mind that Boost has gained way more use and thus reviewed and criticized way more.