Category Archives: OOP

Multiton

Actionscript:
  1. package {
  2.  
  3.     public class Multiton {
  4.        
  5.         public var test:String = "";
  6.        
  7.         private static  var _okToCreate:Boolean;
  8.         private static  var _instances:Object = new Object();
  9.         private static var _instanceNum:int = 0;
  10.  
  11.         public function Multiton() {
  12.             if (_okToCreate) {
  13.                 _instanceNum++;
  14.                 trace("created instance: " +_instanceNum);
  15.             } else {
  16.                 throw new Error(this + " is a Multiton... you must use the getInstance() method to instantiate it.");
  17.             }
  18.         }
  19.         public static function getInstance(key:String="default"):Multiton {
  20.             if (!_instances[key]) {
  21.                 _okToCreate = true;
  22.                 _instances[key] = new Multiton();
  23.                 _okToCreate = false;
  24.             }
  25.             return _instances[key];
  26.         }
  27.     }
  28. }

Holding off on the follow up to yesterdays post because I haven't finished it.... instead, here's a Multiton - Just like a Singleton but with multiple instances. You use it like this:

Actionscript:
  1. var m0:Multiton = Multiton.getInstance("one");
  2. m0.test = "i am instance one";
  3.  
  4. var m1:Multiton = Multiton.getInstance("two");
  5. m1.test = "i am instance two";
  6.  
  7. // elsewhere in the app
  8.  
  9. var multiton:Multiton = Multiton.getInstance("one");
  10. trace(multiton.test);
  11.  
  12. trace(Multiton.getInstance("two").test);

I've used this a few times and I seem to recall the Pure MVC makes use of this pattern (could be wrong about that though).

Posted in OOP | Tagged , | Leave a comment

Function Chaining & Classes

Actionscript:
  1. package {
  2.     import flash.display.Sprite;
  3.     public class Chaining extends Sprite{
  4.         public function Chaining(){
  5.               print(n(100).divide(2).plus(2));
  6.               // outputs 52              
  7.               print(n(100).plus(n(10).multiply(2)));
  8.               // outputs 120
  9.         }
  10.     }
  11. }
  12.  
  13. function print(n:Num):void{
  14.    trace(n.getValue());
  15. }
  16. function n(n:Number):Num{
  17.    return new Num(n);
  18. }
  19.  
  20. class Num{
  21.     private var value:Number;
  22.    
  23.     public function Num(n:Number):void{
  24.        value = n;
  25.     }
  26.     private function convert(n:*):Number{
  27.         if (n is Num) n = n.getValue();
  28.         return n;
  29.     }
  30.     public function getValue():Number{
  31.         return value;
  32.     }
  33.     public function plus(n:*):Num{
  34.         n = convert(n);
  35.         value += n;
  36.         return this;
  37.     }
  38.     public function minus(n:*):Num{
  39.         n = convert(n);
  40.         value -= n;
  41.         return this;
  42.     }
  43.     public function multiply(n:*):Num{
  44.         n = convert(n);
  45.         value *= n;
  46.         return this;
  47.     }
  48.     public function divide(n:*):Num{
  49.         n = convert(n);
  50.         value /= n;
  51.         return this;
  52.     }
  53. }

This snippet is meant to be run as a document class. It shows how one might go about designing a class to make extensive use of function chaining.

Also posted in functions | Tagged , | 2 Comments

var obj:Object = new Function()

Actionscript:
  1. var Pnt:Function = function(xp:Number, yp:Number){
  2.    
  3.     var x:Number = xp;
  4.     var y:Number = yp
  5.    
  6.     this.setX = function(v:int):void{
  7.         x = v;
  8.     }
  9.     this.setY = function(v:int):void{
  10.         y = v;
  11.     }
  12.     this.getX = function():int {
  13.         return x;
  14.     }
  15.     this.getY = function():int {
  16.         return y;
  17.     }
  18. }
  19.  
  20. var p:Object = new Pnt(10,10);
  21.  
  22. trace(p.getX(), p.getY());
  23. p.setX(100);
  24. trace(p.getX());
  25.  
  26. /*
  27. outputs:
  28. 10, 10
  29. 100
  30. */

Another way to define and instantiate Objects on the timeline. Interesting, but I don't recommend it over actual classes...

Also posted in Object, functions | Tagged , | Leave a comment

Object.constructor()

Actionscript:
  1. var Box:Function = {
  2.     constructor : function(color:uint, w:Number, h:Number):void {
  3.         this.color = color;
  4.         this.s = new Shape();
  5.         with(this.s.graphics) beginFill(this.color), drawRect(0,0,w,h);
  6.         addChild(this.s);
  7.        
  8.         this.setLoc = function(x:Number, y:Number):void{
  9.             this.s.x = x;
  10.             this.s.y = y;
  11.        }
  12.     }
  13. }.constructor;
  14.  
  15. var box0:Object = new Box(0xFF0000, 100, 100);
  16.  
  17. box0.setLoc(100, 10);
  18.  
  19. var box1:Object = new Box(0x000000, 50, 50);
  20.  
  21. box1.setLoc(210, 10);

This snippet makes use of Object.constructor() to allow creation of Object instances using the new keyword. This is for fun only, I don't recommend this over actual Classes.

Also posted in dynamic, functions, misc | Tagged , | Leave a comment