Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 916 Bytes

singleton.md

File metadata and controls

35 lines (28 loc) · 916 Bytes

Singleton Pattern

The conventional singleton pattern is really something that is used to overcome the fact that all code must be in a class.

class Singleton {
    private static singleton: Singleton;
    constructor() {
        if (!Singleton.singleton) {
            Singleton.singleton = this;
            // ... any one time initialization goes here ...
        }
        return Singleton.singleton;
    }
    someMethod() { }
}

var something = new Singleton();
var somethingElse = new Singleton();

console.log(something === somethingElse); // true;

However if you don't want lazy initialization you can instead just use a namespace:

namespace Singleton {
    // ... any one time initialization goes here ...
    export function someMethod() { }
}
// Usage
Singleton.someMethod();

Warning : Singleton is just a fancy name for global