Created
June 27, 2012 17:09
-
-
Save makc/3005441 to your computer and use it in GitHub Desktop.
General purpose instance pool class (feel free to improve :)
This file contains hidden or 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 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