| Package | andromeda.util.pool |
| Class | public class ObjectPool |
| Inheritance | ObjectPool CoreObject |
Example :
import andromeda.util.pool.ObjectPool ;
import test.pool.MyBuilder ;
import test.pool.MyClass ;
var i:int;
var pool:ObjectPool = new ObjectPool() ;
pool.allocate( 10 , MyClass , ["hello label"] ) ;
pool.initialize("init", ["arg1", "arg2"]) ;
var activeObjects:Array = [] ;
//read the first object
activeObjects[0] = pool.object;
var k:int = pool.wasteCount ;
trace("pool.wasteCount : " + pool.wasteCount) ; // 9
for ( i = 0 ; i < k ; i++ )
{
activeObjects.push( pool.object ) ; // read the remaining 9 objects
}
// wasteCount is now zero, but usageCount reports 10.
trace("pool.usageCount : " + pool.usageCount) ;
try
{
//this will fail because the pool is now empty
activeObjects.push( pool.object );
}
catch (e:Error)
{
trace(e);
}
k = pool.size ;
// give all objects back to the pool
for (i = 0; i < k; i++)
{
pool.object = activeObjects.shift();
}
// usage count is zero
trace( pool.usageCount ) ;
trace( "======= use grow property" ) ;
pool.grow = true ;
pool.object ; // create a new object and the pool is growing witn 10 new objects inside the buffer.
trace("pool.usageCount : " + pool.usageCount) ; // 1
trace("pool.wasteCount : " + pool.wasteCount) ; // 9
trace( "======= use destroy method" ) ;
pool.destroy() ;
trace("pool.usageCount : " + pool.usageCount) ; // 0
trace("pool.wasteCount : " + pool.wasteCount) ; // 0
trace( "======= Assign a custom object factory, see the test.pool.MyBuilder class") ;
pool.builder = new MyBuilder();
pool.allocate( 20 ) ;
trace( pool.object ) ;
trace("pool.usageCount : " + pool.usageCount) ; // 1
trace("pool.wasteCount : " + pool.wasteCount) ; // 19
| Property | Defined by | ||
|---|---|---|---|
| builder : ObjectPoolBuilder
Defines the builder responsible for creating all pool objects.
| ObjectPool | ||
| grow : Boolean
Indicates if the pool of objects is auto growing when a new user is called with the "object" property.
| ObjectPool | ||
| object : *
Get the next available object from the pool or put it back for the
next use.
| ObjectPool | ||
| parameters : Array
The optional Array representation of parameters to send in the ObjectPoolBuilder.build() method use in the pool to create all objects.
| ObjectPool | ||
| size : int [read-only]
Indicates the pool size.
| ObjectPool | ||
| usageCount : int [read-only]
Indicates the total number of 'checked out' objects currently in use.
| ObjectPool | ||
| wasteCount : int [read-only]
The total number of unused thus wasted objects.
| ObjectPool | ||
| Method | Defined by | ||
|---|---|---|---|
|
ObjectPool(grow:Boolean = false)
Creates a new ObjectPool instance.
| ObjectPool | ||
|
allocate(size:uint = 1, clazz:Class = null, parameters:Array = null):void
Allocates the pool by creating all objects from the builder.
| ObjectPool | ||
|
destroy():void
Destroy and unlock all ressources for the garbage collector.
| ObjectPool | ||
|
flush():void
Removes all unused objects from the pool.
| ObjectPool | ||
![]() |
Returns the internal
ILogger reference of this ILogable object. | CoreObject | |
![]() |
hashCode():uint
Returns a hashcode value for the object.
| CoreObject | |
|
initialize(name:String, args:Array):void
Helper method for applying a function to all objects in the pool.
| ObjectPool | ||
![]() |
Sets the internal
ILogger reference of this ILogable object. | CoreObject | |
![]() |
toSource(indent:int = 0):String
Returns the string representation the source code of the object.
| CoreObject | |
![]() |
toString():String
Returns the string representation of this instance.
| CoreObject | |
| builder | property |
public var builder:ObjectPoolBuilderDefines the builder responsible for creating all pool objects. If you don't want to use a factory, you must provide a class to the allocate method instead.
See also
| grow | property |
public var grow:BooleanIndicates if the pool of objects is auto growing when a new user is called with the "object" property.
| object | property |
object:* [read-write]Get the next available object from the pool or put it back for the next use. If the pool is empty and resizable, an error is thrown.
Implementation public function get object():*
public function set object(value:*):void
| parameters | property |
public var parameters:ArrayThe optional Array representation of parameters to send in the ObjectPoolBuilder.build() method use in the pool to create all objects.
| size | property |
size:int [read-only]Indicates the pool size.
Implementation public function get size():int
| usageCount | property |
usageCount:int [read-only]Indicates the total number of 'checked out' objects currently in use.
Implementation public function get usageCount():int
| wasteCount | property |
wasteCount:int [read-only]The total number of unused thus wasted objects. Use the purge() method to compact the pool.
Implementation public function get wasteCount():int
See also
| ObjectPool | () | constructor |
public function ObjectPool(grow:Boolean = false)Creates a new ObjectPool instance.
Parametersgrow:Boolean (default = false) — Indicates if the pool of objects is auto growing when a new user is called with the "object" property.
|
| allocate | () | method |
public function allocate(size:uint = 1, clazz:Class = null, parameters:Array = null):voidAllocates the pool by creating all objects from the builder.
Parameterssize:uint (default = 1) — The class to create for each object node in the pool.
|
|
clazz:Class (default = null) — The number of objects to create.
|
|
parameters:Array (default = null) — The optional Array representation of parameters to send in the ObjectPoolBuilder.build() method use in this method to create all pooling objects.
This overwrites the current factory.
|
| destroy | () | method |
public function destroy():voidDestroy and unlock all ressources for the garbage collector.
| flush | () | method |
public function flush():voidRemoves all unused objects from the pool. If the number of remaining used objects is smaller than the initial capacity defined by the allocate() method, new objects are created to refill the pool.
| initialize | () | method |
public function initialize(name:String, args:Array):voidHelper method for applying a function to all objects in the pool.
Parametersname:String — The name of the method invoked to initialize all objects in the pool.
|
|
args:Array — The Array representation of all arguments of the init method.
|