Hey guys,
Yesterday I struck out to create a simple API for creating an ObjectPool. There aren’t good examples that I can find of popular APIs people use already, so I would really appreciate help and feedback to discover how you guys would want to have an ObjectPool work.
In Flixel, an FlxGroup
can add()
and remove()
objects, and getFirstAvailable()
for objects that are added, but unused.
I like the idea of being able to easily add and remove objects, but I fear this doesn’t work well if someone wants to have the ObjectPool allocate for them, or have a set size.
I currently have this API, but again, feedback is welcome
var pool = new ObjectPool<Sprite> (function () return new Sprite (), 20);
trace (pool.size); // 20
var sprite1 = pool.get ();
var sprite2 = pool.get ();
pool.release (sprite1);
var sprite3 = pool.create ();
The size
argument is optional. An ObjectPool with no size will allow growth. The get()
method will return null if the size
number of objects is already used. create()
will always return an object, even if it exceeds the number of objects for the pool.
One downside is that when you get()
an object, it’s flagged for use and counts toward the size. If you lose your reference to the object, it remains in the pool forever.
If we were to not use a size, we could have get()
return an object, and the pool could not retain a reference. If you get()
and forget to release()
, at least it would be garbage collected, though you might miss that you are causing extra GC activity this way.
Please help me know if you think we should change this (before it is released in Lime), if both create()
and get()
is good, name preferences, etc, etc
Thank you