while trying to comeup with a pooling class for instances of objects i have come accross a question of design.. im not sure which i should use and i am curious of some opinions..
note:: that this code as of now is not compileable but should be enough to get the ideas accross
The Pool and Prototype classes that dont change
the purpose of the pool is to store copy of objects by a unique name and when asked for retreval to send a copy of the data to the ojects requesting it
// THE POOL
template<typename T>
class Pool
{
protected:
std::vector< Prototype<T>*> m_pool;
public:
Pool(void);
virtual ~Pool(void);
// adds to the pool list
virtual bool Add(T * pT, char * pName) = 0;
bool Get(T * pT, char* pName){ /* copy the prototpye by name in to the <T> <- making a unique copy*/}
};
/// THE Prototpye class :: only usable by Pool class
template<typename T>
class Prototype
{
friend class Pool<T>;
protected:
T* m_pType;
char * m_pName;
Prototype(T * pT, char* pName);
virtual ~Prototype(void);
};
idea #1
in the example the class to be "prototypeable" inherits the Prototype<t>
class Character : public Prototype<Character>
{
public
char* p_Name;
/*
blah
blah
*/
};
class CharacterPool : public Pool<Character>
{
public
CharacterPool()
~CharacterPool()
bool Add(Character * pCharacter, char * pName){m_pool.push_back(pCharacter,pName );}
}
idea #2
here the character does not inherit the prototype<t> but when the character is added to the pool it gets put into a prototype
class Character
{
public
char* p_Name;
/*
blah
blah
*/
};
class CharacterPool : public Pool<Character>
{
public
CharacterPool()
~CharacterPool()
bool Add(Character * pCharacter, char * pName){m_pool.push_back( new Prototype<Character>(pCharacter,pName ));} // <--- see
}
I can see how both of the situations would work...
as of now i am leaning toward idea#2 because i dont really want the overhead of the inheritance of the virtual stuff...
seeing as how the only place the prototype is actually used is in the pool and will not be used that often...
hmm i hope i made some sense
ideas / other solutions