The point of the pool of objects is usually to avoid the overhead of distributing and freeing the system, and requires that you know the βworstβ limit on the amount of each type of object that you want to allocate. Therefore, assuming that your entities are different in life and use, you probably want to have separate pools for each object. (You can definitely improve the pool, as in @zaske's answer.)
On the other hand, if you really need a single Entity pool, and you want to configure these objects at runtime, you can move from using Entity subclasses to something like:
class Entity { EntityCustomizer custom; ... }
Then, choosing Entity from the pool, you pass Bullet or Ship or any other customizer:
public Entity obtain (int objectType) { Entity ent = super.obtain(); if (objectType==SOME_OBJECT_TYPE) ent.custom = EntityCustomizer.SOME_OBJECT_TYPE_IMPL; return ent; }
(I am sure this is a well-known pattern, and I should use the appropriate language to identify the parts ...)
All of this, I think that separate pools for each Entity type are the way to go, since my work is pretty negligent (you trade a bunch of compilation type checks for some flexibility at runtime).
PT
source share