Instantiating a Display Object is a fairly heavy lift. Usually this is negligible, but when dealing with particle or fluid effects, it can add up. Here’s one implementation of the Factory pattern used with Display Objects, to minimize the total number created – each Display Object would then have it’s visible properties and behaviors updated.

AS3 is also nice enough to let us dynamically type the returns (unlike Java), so we can even specify the Class of the View to be produced.

package org.upshots.display {

import flash.display.DisplayObject;
import flash.display.Sprite;

public class ViewFactory {

private var employed:Array = [];
private var unemployed:Array = [];
private var kind:Class = Sprite;

public function ViewFactory(type:Class):void {
this.kind = type;

public function employ():DisplayObject {
var b:DisplayObject;
if(unemployed.length > 0){
b = unemployed.pop();
} else {
b = new this.kind();
return this.kind(b);

public function retire(b:DisplayObject):void {
var n:int = employed.indexOf(b);
if(n > -1){
employed.splice(n, 1);

Usage might look like this:
var factory:ViewFactory = new ViewFactory(Sprite);
var sprite:Sprite = Sprite(factory.employ());

Rather than the more traditional “new” operation:
var sprite:Sprite = new Sprite();