设计模式之-单例模式

1.创建一个单例类

javascript 复制代码
class Singleton {
    constructor(name){
        this.name=name;
    }
    static instance = null;
    getName(){
        console.log(this.name);
    }
    static getInstance(name){
        if(!Singleton.instance){
            Singleton.instance = new Singleton(name);
        }
        return Singleton.instance;
    }
}
const a = Singleton.getInstance('a');
const b=Singleton.getInstance('b');
console.log(a===b) // true

2.我们通过Singleton.getInstance来获取Singleton类的唯一对象,这种方式相对简单,但有一个问题,就是增加了这个类的'不透明性',Singleton类的使用者必须知道这是一个单例类,跟以往通过new的方式不同,这里便要用Singleton.getInstance来获取对象。

3.透明的单例模式

javascript 复制代码
 		class CreateDiv{
            static instance=null;
            constructor(html){
                if(CreateDiv.instance){
                    return CreateDiv.instance;
                }
                this.html=html;
                this.init();
                return CreateDiv.instance=this;
            }
            init(){
                const div=document.createElement('div');
                div.innerHTML=this.html;
                document.body.appendChild(div);
            }
        }

        const aa=new CreateDiv('aa');
        const bb=new CreateDiv('bb');
        console.log(aa===bb) // true

4.虽然现在完成了一个透明的单例类的编写,但是他同样又一些缺点,在这段代码中,CreateDiv的构造器实际上负责了两件事情。一个是创建对象和执行初始化函数,第二个是保证只有一个对象,这是违反"单一职责原则的",将来如果我们需要利用这个累,在页面上创建千千万万个div,即要让这个类从单例类变成一个普通的可以产生多个实例的类,那么我们必须改写CreateDiv这个类,把控制创建为宜对象的那一段去掉,这种修改会给我们带来不必要的烦恼

5.用代理实现单例模式

javascript 复制代码
		class CreateDiv{
            static instance=null;
            constructor(html){
                this.html=html;
                this.init();
            }
            init(){
                const div=document.createElement('div');
                div.innerHTML=this.html;
                document.body.appendChild(div);
            }
        }

        class ProxySingletonCreateDiv {
            static instance=null;
            constructor(html){
                if(!ProxySingletonCreateDiv.instance){
                    ProxySingletonCreateDiv.instance = new CreateDiv(html)
                }
                return ProxySingletonCreateDiv.instance;
            }
        }
        const aa=new ProxySingletonCreateDiv('aa');
        const bb=new ProxySingletonCreateDiv('bb');
        console.log(aa===bb) // true

6.通过引入代理类的方式,我们同样完成了一个单例模式的编写,跟之前不同的是,现在我们把负责管理单例的逻辑转移到了代理类ProxySingletonCreateDiv中,这样一来,CreateDiv就变成了一个普通的类,他跟ProxySingletonCreateDiv组合起来就可以达到单例类的效果,这个例子也是缓存代理的应用之一

相关推荐
静水流深_沧海一粟10 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder10 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室17 小时前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo5 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4965 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
驴儿响叮当20105 天前
设计模式之状态模式
设计模式·状态模式
电子科技圈5 天前
XMOS推动智能音频等媒体处理技术从嵌入式系统转向全新边缘计算
人工智能·mcu·物联网·设计模式·音视频·边缘计算·iot
徐先生 @_@|||5 天前
安装依赖三方exe/msi的软件设计模式
设计模式