Skip to content

Instantly share code, notes, and snippets.

@FrancescoMaisto
Last active December 10, 2015 16:28
Show Gist options
  • Save FrancescoMaisto/4460952 to your computer and use it in GitHub Desktop.
Save FrancescoMaisto/4460952 to your computer and use it in GitHub Desktop.
package
{
public final class ObjectPool
{
private var MAX_VALUE:uint;
private var GROWTH_VALUE:uint;
private var TYPE:Class;
private var counter:uint;
private var pool:Array;
public function ObjectPool(type:Class, maxPoolSize:uint, growthValue:uint)
{
MAX_VALUE = maxPoolSize;
GROWTH_VALUE = growthValue;
TYPE = type;
counter = maxPoolSize;
var i:uint = maxPoolSize;
pool = new Array();
while (--i > -1)
pool[i] = new type();
}
public function getObject():*
{
if (counter > 0) return pool[--counter];
var i:uint = GROWTH_VALUE;
while( --i > -1 )
pool.unshift ( new TYPE() );
counter = GROWTH_VALUE;
return getObject();
}
public function returnObject(disposedObject:*):void
{
pool[counter++] = disposedObject;
}
public function numberOfObjects():uint
{
return counter;
}
}
}
@JulianG
Copy link

JulianG commented Jan 5, 2013

I can see how this one is better, although more complex than mine.
Here you remove the object from the pool, and its the "user"'s responsibility to return it to the pool later.
My simpler script may break if the user requests more objects than the pool holds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment