Packageandromeda.util.pool
Classpublic class ObjectPool
InheritanceObjectPool Inheritance CoreObject

This class implements the object pool design pattern implementation.

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   
     



Public Properties
 PropertyDefined 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
Public Methods
 MethodDefined 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
 Inherited
Returns the internal ILogger reference of this ILogable object.
CoreObject
 Inherited
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
 Inherited
setLogger(log:ILogger = null):void
Sets the internal ILogger reference of this ILogable object.
CoreObject
 Inherited
toSource(indent:int = 0):String
Returns the string representation the source code of the object.
CoreObject
 Inherited
toString():String
Returns the string representation of this instance.
CoreObject
Property detail
builderproperty
public var builder:ObjectPoolBuilder

Defines 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

growproperty 
public var grow:Boolean

Indicates if the pool of objects is auto growing when a new user is called with the "object" property.

objectproperty 
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
parametersproperty 
public var parameters:Array

The optional Array representation of parameters to send in the ObjectPoolBuilder.build() method use in the pool to create all objects.

sizeproperty 
size:int  [read-only]

Indicates the pool size.

Implementation
    public function get size():int
usageCountproperty 
usageCount:int  [read-only]

Indicates the total number of 'checked out' objects currently in use.

Implementation
    public function get usageCount():int
wasteCountproperty 
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

Constructor detail
ObjectPool()constructor
public function ObjectPool(grow:Boolean = false)

Creates a new ObjectPool instance.

Parameters
grow:Boolean (default = false) — Indicates if the pool of objects is auto growing when a new user is called with the "object" property.
Method detail
allocate()method
public function allocate(size:uint = 1, clazz:Class = null, parameters:Array = null):void

Allocates the pool by creating all objects from the builder.

Parameters
size: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():void

Destroy and unlock all ressources for the garbage collector.

flush()method 
public function flush():void

Removes 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):void

Helper method for applying a function to all objects in the pool.

Parameters
name: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.