How to define Singleton in TypeScript

前端 未结 20 668

What is the best and most convenient way to implement a Singleton pattern for a class in TypeScript? (Both with and without lazy initialisation).

相关标签:
20条回答
  • 2020-11-28 20:46

    You can use class expressions for this (as of 1.6 I believe).

    var x = new (class {
        /* ... lots of singleton logic ... */
        public someMethod() { ... }
    })();
    

    or with the name if your class needs to access its type internally

    var x = new (class Singleton {
        /* ... lots of singleton logic ... */
        public someMethod(): Singleton { ... }
    })();
    

    Another option is to use a local class inside of your singleton using some static members

    class Singleton {
    
        private static _instance;
        public static get instance() {
    
            class InternalSingleton {
                someMethod() { }
    
                //more singleton logic
            }
    
            if(!Singleton._instance) {
                Singleton._instance = new InternalSingleton();
            }
    
            return <InternalSingleton>Singleton._instance;
        }
    }
    
    var x = Singleton.instance;
    x.someMethod();
    
    0 讨论(0)
  • 2020-11-28 20:48

    I am surprised not to see the following pattern here, which actually looks very simple.

    // shout.ts
    class ShoutSingleton {
      helloWorld() { return 'hi'; }
    }
    
    export let Shout = new ShoutSingleton();
    

    Usage

    import { Shout } from './shout';
    Shout.helloWorld();
    
    0 讨论(0)
  • 2020-11-28 20:48

    Add the following 6 lines to any class to make it "Singleton".

    class MySingleton
    {
        private constructor(){ /* ... */}
        private static _instance: MySingleton;
        public static getInstance(): MySingleton
        {
            return this._instance || (this._instance = new this());
        };
    }
    

    Test example:

    var test = MySingleton.getInstance(); // will create the first instance
    var test2 = MySingleton.getInstance(); // will return the first instance
    alert(test === test2); // true
    

    [Edit]: Use Alex answer if you prefer to get the instance through a property rather a method.

    0 讨论(0)
  • 2020-11-28 20:49

    Since TS 2.0, we have the ability to define visibility modifiers on constructors, so now we can do singletons in TypeScript just like we are used to from other languages.

    Example given:

    class MyClass
    {
        private static _instance: MyClass;
    
        private constructor()
        {
            //...
        }
    
        public static get Instance()
        {
            // Do you need arguments? Make it a regular static method instead.
            return this._instance || (this._instance = new this());
        }
    }
    
    const myClassInstance = MyClass.Instance;
    

    Thank you @Drenai for pointing out that if you write code using the raw compiled javascript you will not have protection against multiple instantiation, as the constraints of TS disappears and the constructor won't be hidden.

    0 讨论(0)
  • 2020-11-28 20:50

    My solution for it:

    export default class Modal {
        private static _instance : Modal = new Modal();
    
        constructor () {
            if (Modal._instance) 
                throw new Error("Use Modal.instance");
            Modal._instance = this;
        }
    
        static get instance () {
            return Modal._instance;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 20:54

    Here is yet another way to do it with a more conventional javascript approach using an IFFE:

    module App.Counter {
        export var Instance = (() => {
            var i = 0;
            return {
                increment: (): void => {
                    i++;
                },
                getCount: (): number => {
                    return i;
                }
            }
        })();
    }
    
    module App {
        export function countStuff() {
            App.Counter.Instance.increment();
            App.Counter.Instance.increment();
            alert(App.Counter.Instance.getCount());
        }
    }
    
    App.countStuff();
    

    View a demo

    0 讨论(0)
提交回复
热议问题