Skip to content

Instantly share code, notes, and snippets.

@makc
Created June 27, 2012 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makc/3005441 to your computer and use it in GitHub Desktop.
Save makc/3005441 to your computer and use it in GitHub Desktop.
General purpose instance pool class (feel free to improve :)
package com.realaxy.operators{
import flash.utils.Dictionary;
internal class Pool {
private var instances : Dictionary =new Dictionary() ;
private var instanceCounts : Dictionary =new Dictionary() ;
public function getInstance ( type : Class ) : * {
var cache : Vector.<*>;
if ( instanceCounts[type] ) {
cache = instances[type];
}else{
cache = new Vector.<*>();
instances[type] = cache;
instanceCounts[type] = 0;
}
if ( instanceCounts[type] < cache.length ) {
return cache[instanceCounts[type]++];
}else{
var object : * =new type();
cache[instanceCounts[type]++] = object;
return object;
}
}
public function resetCounts ( type : Class ) : void {
if ( instanceCounts[type] ) {
instanceCounts[type] = 0;
}
}
public function resetAllCounts ( ) : void {
for ( var type : Object in instanceCounts ) {
instanceCounts[type] = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment