How do I implement a Singleton pattern ?

Hey all !

Wondering if there is any way to implement a singleton pattern ?

Thank you !

LeGmask

  • This worked for me:

    class Singleton {
        private var val;
        private static var instance;
    
        private function initialize() {
            val = 42;
        }
    
        static function getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    
        function getVal() {
            return val;
        }
    }
    
    function testSingleton() {
        var s = Singleton.getInstance();
        System.println("s.getVal() = " + s.getVal());
    
    }