Last active
December 10, 2015 16:28
-
-
Save FrancescoMaisto/4460952 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.