Packagevegas.utils.pool
Classpublic class ObjectPool
InheritanceObjectPool Inheritance Object

This class implements the object pool design pattern implementation.

View the examples



Public Properties
 PropertyDefined By
  builder : ObjectPoolBuilder
Defines the builder responsible for creating all pool objects.
ObjectPool
  growing : Boolean
Indicates if the pool of objects is auto growing when a new user is called with the "object" property.
ObjectPool
  length : int
[read-only] Indicates the pool size.
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
  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(growing: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
  
dispose(o:*):void
Puts it back for the next use the specified object.
ObjectPool
  
flush():void
Removes all unused objects from the pool.
ObjectPool
  
get():*
Returns the next available object from the pool.
ObjectPool
  
initialize(name:String, args:Array):void
Helper method for applying a function to all objects in the pool.
ObjectPool
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

growingproperty 
public var growing:Boolean

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

lengthproperty 
length:int  [read-only]

Indicates the pool size.


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

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(growing:Boolean = false)

Creates a new ObjectPool instance.

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

dispose()method 
public function dispose(o:*):void

Puts it back for the next use the specified object.

Parameters

o:*

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.

get()method 
public function get():*

Returns the next available object from the pool.

Returns
*

Throws
Error — If the pool is empty and resizable, an error is thrown.
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.

Examples
Example
     
     import vegas.util.pool.ObjectPool ;
     
     import examples.pool.MyBuilder ;
     import examples.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.get() ;
     
     var k:int = pool.wasteCount ;
     
     trace("pool.wasteCount : " + pool.wasteCount) ; // 9
     
     for ( i = 0 ; i < k ; i++ )
     {
         activeObjects.push( pool.get() ) ; // 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.get() );
     }
     catch (e:Error)
     {
         trace(e);
     }
     
     k = pool.size ;
     
     // give all objects back to the pool
     
     for (i = 0; i < k; i++)
     {
         pool.dispose( activeObjects.shift() ) ;
     }
     
     // usage count is zero
     
     trace( pool.usageCount ) ;
     
     trace( "======= use grow property" ) ;
     
     pool.grow = true ;
     
     pool.get() ; // 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