Nov
19
2009
Object pool example
Object pools are valuable when it comes to resource management and garbage collection. Rather than creating many object of the same type using the "new" operator, which is expensive in terms of performance you can reuse your objects. Object pools keep referenes to previously created objects in the list and objects that are retired are kept there. If object is needed again, a reference is returned which is a less intensive task than creating it (instantiating) again in a memory.
//use this function to get your object (instead of using new operator) function getThingInstance(param:int):Thing { if (pool.length) { obj = pool.pop(); } else { obj = new Thing(); pool.push(obj); } obj.init(param); return obj; } //to retire the object, return it back to the pool function returnThingInstance(obj:Thing) { pool.push(obj); }
Add comment

