nicenote/docs/fea/pattern/singleton.md
2021-08-25 20:10:25 +08:00

613 B

nav group
title path
前端 /fea
title order
💊 设计模式 2

单例模式

一个类只有一个实例,并提供一个访问它的全局访问点。

// ------------------- 单例模式 -------------------
export default class Singleton {
    constructor(name, creator, products) {
        this.name = name
        this.creator = creator
        this.products = products
    }

    static getInstance(name, creator, products) {
        if (!this.instance) {
            this.instance = new Singleton(name, creator, products)
        }
        return this.instance
    }
}