TITLE: Factories for creating objects [ We join a discussion started by someone who wanted to map enums to different classes (to act as a type id. -adc ] PROBLEM: ericg@hum.ucsc.edu (Eric Goodman) One quick thought would be to create a class to allocate the objects: class XXX_allocator { public: XXX_A* create_XXX_A(); XXX_B* create_XXX_B(); . . . }; Doing this, you could actually keep the enumerations in the allocator class, and the abstract classes are less tightly coupled to the derived classes, in the sense that adding a derived class means modifying only the allocator class, not all of the actual base classes. You would then create a global allocator object, and get all objects through it. RESPONSE: rmartin@rcmcon.com (Robert Martin), 13 Dec 93 Now take this one step farther. Create an abstract base class for the allocator class (lets call it a factory). This will allow you to do two things: First you can create polymophic instances of the factory classes. Each instance represents a different mapping of the enueration to the created concrete classes. Secondly since all object creation would now be accomplished through this abstract base, no part of your application (with the exception of main) will have a compile time dependency upon the concrete classes. Consider, a non-abstract allocator must #include the headers for all the concrete classes that it can allocate. Any module which uses that allocator will therefore depend upon those concrete header files. Not only does this mean that compile times increase, but also that you must recompile the world whenever you change one of the concrete header files. However if you use an abstract factory, then none of the modules that use the allocator will depend upon the concrete headers. Thus you can make changes to them without having to recompile the world. This technique is written up in an an ECOOP-93 article named "Design Patterns: Abstraction and Reuse of Object-Oriented Design" by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides. [ We here at SES discovered this design technique indenpendently. We use the edge creators and derived ob/wb creators to achieve this result. -adc ]