|
Location: Desktop development - C/C++ License: The Intelliproject Open License (IPOL) Generic Derived Class Factory in C++Posted by Mark Jundo P. DocumentoA reusable C++ implementation of a factory for derived classes |
Skill: IntermediatePosted: 02/03/2009Views: 2455Rating: 5.00 /5Popularity: 1.51 |
| Sign Up to vote for this article |
The goal of this article is to create a reusable class based on the Abstract Factory Pattern though, for some reason I can't remember, I chose to name it DerivedClassFactory. The design of this factory should allow us to register classes without changing anything within the factory itself. Another goal is to create a factory where the id used to generate the classes can be customized using C++ templates. The class also offers run time type safety with the help of templates and the dynamic_cast operator. We will be using the STL container map to keep our registration information, since STL comes with any decent C++ compiler and it is after all, a C++ standard.
The design of our factory has the following straightforward specifications:
Based on this, we would have something like:
The registry member variable holds our registration entry. This is a std::map object with template the class parameter _ID_ as key and a pointer to ClassFactoryFunc as value types respectively. We have defined ClassFactoryFunc as a function that returns a dynamically created instance of our base class, which is the class template parameter _BASE_.
At the heart of the factory is a generic function that actually instantiates the derived class we intend to register.
This is a static function that will provide us with a ClassFactoryFunc pointer that we can store in our registry. Type safey is acheived by dynamic_casting the derived type pointer to our base class.
We now add the method to register a class:
To create or instantiate our registered classes we then use the following method:
which is just a matter of searching for the id we want in the registry and executing the function pointer that we stored earlier.
To use our factory we need to instantiate it with the template parameters _ID_ and _BASE_ which is our id and base class types respectively:
To register classes we just use the registerClass function:
Or we could use the a macro that does just what the above code does:
To create an object:
The extra delete in derivedClassFactoryFunc is a pain if you are dealing with large classes. But, it is necessary and should not happen if you write your code with forethought and care.
The Abstract Factory pattern is a common pattern in software engineering. Harnessing the power of templates in C++ allowed us to create a factory that is simple, type-safe, reusable and allows us to register classes without changing the factory class code itself.
This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)
| Mark Jundo P. Documento
| Location: |
Sign up to post message on the article message board!