forked from: Singleton

by mi77 forked from Singleton (diff: 21)
♥0 | Line 33 | Modified 2010-01-28 14:55:12 | MIT License
play

ActionScript3 source code

/**
 * Copyright mi77 ( http://wonderfl.net/user/mi77 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aSkQ
 */

// forked from kamipoo's Singleton
package {
    import flash.display.Sprite;
    import flash.text.*;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            var singleton:Singleton = Singleton.getInstance();

           // 本当にSingletonになってるかな
           ////////////////////////////////////////////////////////////
            var singleton2:Singleton = Singleton.getInstance();
            var tf = addChild(new TextField())  
            tf.width=500;
            
            tf.text = String(singleton);
         
            tf.appendText("\n"+String(singleton2));
            
            tf.appendText("\n\nあれれ? これだと何個でもつくれちゃう?");
            
            tf.appendText("\n\nいや。それでいいのだ。");
            
           ////////////////////////////////////////////////////////////
        }
    }
}

class Singleton {
    private static var _INTERNAL_CALL:Boolean = false;
    private static var _INSTANCE:Singleton;
    public function Singleton() {
        if(!_INTERNAL_CALL) {
            new ArgumentError("error");
        }
    }
    public static function getInstance():Singleton {
        if(!_INSTANCE) {
            _INTERNAL_CALL = true;
            _INSTANCE = new Singleton();
            _INTERNAL_CALL = false;
        }
        return _INSTANCE;
    }
    
}