🎞️ ACTIONSCRIPT WARD DIFFICULTY: Adept Structural

The Flyweight

Conserving precious AVM2 memory by sharing intrinsic state among a swarm of locusts.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Transmutation // Memory Optimization
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// ACTIONSCRIPT PATTERN //
package arcana.flyweight {
    import flash.display.BitmapData;
    import flash.geom.Point;

    // Intrinsic State (Shared)
    public class LocustSprite {
        public var bitmapData:BitmapData;

        public function LocustSprite() {
            // A massive horrific bitmap loaded once
            bitmapData = new BitmapData(32, 32, true, 0xFF000000);
        }
    }

    // The Factory
    public class SwarmFactory {
        private var sharedLocust:LocustSprite;

        public function getLocustSprite():LocustSprite {
            if (!sharedLocust) {
                sharedLocust = new LocustSprite();
                trace("Forged the original locust template.");
            }
            return sharedLocust;
        }
    }

    // Extrinsic State (Unique)
    public class SwarmLocust {
        private var sprite:LocustSprite;
        public var x:Number;
        public var y:Number;

        public function SwarmLocust(sprite:LocustSprite, x:Number, y:Number) {
            this.sprite = sprite;
            this.x = x;
            this.y = y;
        }

        public function render():void {
            // Would draw sprite.bitmapData at x, y using copyPixels
            trace("Drawing locust at " + x + ", " + y);
        }
    }
}
Runic Tags: #flyweight #actionscript #flash #optimization