I have a class that stores points in an n-dimensional space. It has an integer, n, for the number of dimensions, and a pointer into an array for the distance along each axis. The program it is in creates and destroys many of these as it goes. To prevent memory fragmentation I use memory pools. I have 'class pool' for the class itself and an 'array pool' for each length of array.
In handing creating these, the sequence would be: get a copy of the overall class from the first memory pool, using the dimensionality (n) the class is being constructed with determine the correct 'array pool' is, or if it does not exist, create it, get a pointer to the memory segment for the class, and then return the whole thing. Essentially, the class is always the same, what varies is the size of its internal array and which pool the internal array came from.
I can think of a couple of ways to handle this but the one that I am gravitating towards is to write a factory class which handles all of that.
Is this the appropriate pattern to choose? Is there a more preferred method?