實(shí)現(xiàn)一個(gè)ts單例模式基類(支持代碼提示、禁止二次實(shí)例化)

另一種實(shí)現(xiàn)方式的傳送門(mén):【TS】另一種實(shí)現(xiàn)typescript單例模式的方式(支持代碼提示,禁止二次實(shí)例化) - 簡(jiǎn)書(shū) (jianshu.com)

先貼出我的代碼,后面再講講為什么要這么做:

class Singleton {
    // 實(shí)例
    private static _instance: Singleton;
    // 是否是通過(guò)getInstance實(shí)例化
    private static _instantiateByGetInstance: boolean = false;
    
    /**
     * 獲取實(shí)例
     */
    public static getInstance<T extends Singleton>(this: (new () => T) | typeof Singleton): T {
        const _class = this as typeof Singleton;
        if (!_class._instance) {
            _class._instantiateByGetInstance = true;
            _class._instance = new _class();
            _class._instantiateByGetInstance = false;
        }
        return _class._instance as T;
    }
    
    /**
     * 構(gòu)造函數(shù)
     * @protected
     */
    protected constructor() {
        if (!(this.constructor as typeof Singleton)._instantiateByGetInstance) {
            throw new Error("Singleton class can't be instantiated more than once.");
        }
    }
}

我在網(wǎng)上搜索了一遍,大多數(shù)文章里的ts單例模式或多或少是不夠完美的,我的單例模式有以下幾個(gè)優(yōu)點(diǎn):

  1. 子類可以直接繼承此Singleton類,無(wú)需其他多余代碼,比如
class TestClass extends Singleton {
    
}
  1. 支持IDE代碼提示
class TestClass extends Singleton {
    testFunc() {
        
    }
}
代碼提示
  1. 禁止使用new關(guān)鍵字實(shí)例化


    禁止實(shí)例化

接下來(lái)講講為什么要這么寫(xiě)

  • getInstance有泛型參數(shù)T,它需要繼承Singleton,使用的時(shí)候不需要添加泛型參數(shù),當(dāng)然加上也沒(méi)問(wèn)題,比如:TestClass.getInstance<TestClass>()
  • ts函數(shù)參數(shù)列表中的第一項(xiàng)可以為this。這個(gè)this只是用于手動(dòng)告訴ts編譯器它的類型,實(shí)際并不需要傳入。
  • this參數(shù)的類型為(new () => T) | typeof Singleton聯(lián)合類型。new () => T表示某個(gè)構(gòu)造函數(shù)類型,由于TestClass繼承自Singleton,同時(shí)getInstance是靜態(tài)方法,所以這里的new () => T代表的是TestClass類。使用了new () => T,就能告訴編輯器當(dāng)前使用了哪個(gè)類,但是這樣一來(lái)構(gòu)造函數(shù)使用protected或者private修飾時(shí)會(huì)在編譯階段報(bào)錯(cuò),可能會(huì)提示Cannot assign a 'protected' constructor type to a 'public' constructor type.,意思是無(wú)法將“protected”構(gòu)造函數(shù)類型分配給“public”構(gòu)造函數(shù)類型。這時(shí)候我們可以指定this的類型為(new () => T) | typeof Singleton聯(lián)合類型。typeof Singleton表示Singleton類的類型,因?yàn)樵陬惖膬?nèi)部可以實(shí)例化受保護(hù)的或者私有的構(gòu)造函數(shù),從而規(guī)避掉上述錯(cuò)誤。
  • 將構(gòu)造函數(shù)用protected修飾依然無(wú)法避免某些大聰明在子類內(nèi)部實(shí)例化,所以構(gòu)造函數(shù)內(nèi)部使用了_instantiateByGetInstance判斷是否是通過(guò)getInstance方法進(jìn)行實(shí)例化的。當(dāng)然這個(gè)問(wèn)題只能在運(yùn)行期間被發(fā)現(xiàn)。
class TestClass extends Singleton {
    testFunc() {
        new TestClass();
    }
}
拋出錯(cuò)誤
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容