【JavaScript设计模式】单例模式

单例模式

介绍

保证一个类仅有一个实例,并提供一个访问它的全局访问点

相当于我们在应用程序中共享一个全局实例


单例模式的实现思路

如何才能保证一个类仅有一个实例?
一般情况下,当我们创建了一个类(本质是构造函数)后,可以通过new关键字调用构造函数进而生成任意多的实例对象。

class SingleDog {
    
    
    show() {
    
    
        console.log('我是一个单例对象')
    }
}

const s1 = new SingleDog()
const s2 = new SingleDog()

// false
s1 === s2

s1和s2两者是相互独立的对象,各占一块内存空间。而单例模式想要做到的是,不管我们尝试去创建多少次,它都只给你返回第一次所创建的那唯一的一个实例

要做到这一点,就需要构造函数具备判断自己是否已经创建过一个实例的能力。

class SingleDog {
    
    
    show() {
    
    
        console.log('我是一个单例对象')
    }
    static getInstance() {
    
    
        // 判断是否已经new过1个实例
        if (!SingleDog.instance) {
    
    
            // 若这个唯一的实例不存在,那么先创建它
            SingleDog.instance = new SingleDog()
        }
        // 如果这个唯一的实例已经存在,则直接返回
        return SingleDog.instance
    }
}

const s1 = SingleDog.getInstance()
const s2 = SingleDog.getInstance()

// true
s1 === s2

上面的这种方法是当创建了一个实例后,会给SingleDog类增加一个属性instance,当我们二次创建时,会首先判断是否有这个属性,如果有,直接return

还有一种方法是:使用一个标志变量来判断。

SingleDog.getInstance = (function() {
    
    
    // 定义自由变量instance,模拟私有变量
    let instance = null
    return function() {
    
    
        // 判断自由变量是否为null
        if(!instance) {
    
    
            // 如果为null则new出唯一实例
            instance = new SingleDog()
        }
        return instance
    }
})()

生产实践:Vuex中的单例模式

一个 Vue 实例只能对应一个 Store

如何在项目中引入 Vuex:

// 安装vuex插件
Vue.use(Vuex)

// 将store注入到Vue实例中
new Vue({
    
    
    el: '#app',
    store
})

通过调用Vue.use()方法,我们安装了 Vuex 插件。Vuex 插件是一个对象,它在内部实现了一个 install 方法,这个方法会在插件安装时被调用,从而把 Store 注入到Vue实例里去。也就是说每 install 一次,都会尝试给 Vue 实例注入一个 Store。

在 install 方法里,有一段逻辑和我们楼上的 getInstance 非常相似的逻辑:

let Vue // 这个Vue的作用和楼上的instance作用一样,作为一个标志变量
...

export function install (_Vue) {
    
    
  // 判断传入的Vue实例对象是否已经被install过Vuex插件(是否有了唯一的state)
  if (Vue && _Vue === Vue) {
    
    
    if (process.env.NODE_ENV !== 'production') {
    
    
      console.error(
        '[vuex] already installed. Vue.use(Vuex) should be called only once.'
      )
    }
    return
  }
  // 若没有,则为这个Vue实例对象install一个唯一的Vuex
  Vue = _Vue
  // 将Vuex的初始化逻辑写进Vue的钩子函数里
  applyMixin(Vue)
}

楼上便是 Vuex 源码中单例模式的实现办法了。通过这种方式,可以保证一个 Vue 实例(即一个 Vue 应用)只会被 install 一次 Vuex 插件,所以每个 Vue 实例只会拥有一个全局的 Store。


使用单例模式实现模态框

实现一个全局唯一的Modal弹框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单例模式弹框</title>
</head>
<style>
    #modal {
      
      
        height: 200px;
        width: 200px;
        line-height: 200px;
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        border: 1px solid black;
        text-align: center;
    }
</style>
<body>
	<button id='open'>打开弹框</button>
	<button id='close'>关闭弹框</button>
</body>
<script>
    // 核心逻辑,这里采用了闭包思路来实现单例模式
    const Modal = (function() {
      
      
    	let modal = null
    	return function() {
      
      
            if(!modal) {
      
      
            	modal = document.createElement('div')
            	modal.innerHTML = '我是一个全局唯一的Modal'
            	modal.id = 'modal'
            	modal.style.display = 'none'
            	document.body.appendChild(modal)
            }
            return modal
    	}
    })()
    
    // 点击打开按钮展示模态框
    document.getElementById('open').addEventListener('click', function() {
      
      
        // 未点击则不创建modal实例,避免不必要的内存占用
    	const modal = new Modal()
    	modal.style.display = 'block'
    })
    
    // 点击关闭按钮隐藏模态框
    document.getElementById('close').addEventListener('click', function() {
      
      
    	const modal = new Modal()
    	if(modal) {
      
      
    	    modal.style.display = 'none'
    	}
    })
</script>
</html>

实现唯一的Storage

实现Storage,使得该对象为单例,基于 localStorage 进行封装。实现方法 setItem(key,value) 和 getItem(key)。

静态方法版

// 定义Storage
class Storage {
    
    
    static getInstance() {
    
    
        // 判断是否已经new过1个实例
        if (!Storage.instance) {
    
    
            // 若这个唯一的实例不存在,那么先创建它
            Storage.instance = new Storage()
        }
        // 如果这个唯一的实例已经存在,则直接返回
        return Storage.instance
    }
    getItem (key) {
    
    
        return localStorage.getItem(key)
    }
    setItem (key, value) {
    
    
        return localStorage.setItem(key, value)
    }
}

const storage1 = Storage.getInstance()
const storage2 = Storage.getInstance()

storage1.setItem('name', '李雷')
// 李雷
storage1.getItem('name')
// 也是李雷
storage2.getItem('name')

// 返回true
storage1 === storage2

闭包版

惰性单例

// 先实现一个基础的StorageBase类,把getItem和setItem方法放在它的原型链上
function StorageBase () {
    
    }
StorageBase.prototype.getItem = function (key){
    
    
    return localStorage.getItem(key)
}
StorageBase.prototype.setItem = function (key, value) {
    
    
    return localStorage.setItem(key, value)
}

// 以闭包的形式创建一个引用自由变量的构造函数
const Storage = (function(){
    
    
    let instance = null
    return function(){
    
    
        // 判断自由变量是否为null
        if(!instance) {
    
    
            // 如果为null则new出唯一实例
            instance = new StorageBase()
        }
        return instance
    }
})()

// 这里其实不用 new Storage 的形式调用,直接 Storage() 也会有一样的效果 
const storage1 = new Storage()
const storage2 = new Storage()

storage1.setItem('name', '李雷')
// 李雷
storage1.getItem('name')
// 也是李雷
storage2.getItem('name')

// 返回true
storage1 === storage2

参考文章:

JavaScript 设计模式核⼼原理与应⽤实践

猜你喜欢

转载自blog.csdn.net/weixin_52834435/article/details/127184049