-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
面试官:说说你对单例模式的理解?如何实现? #291
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一、是什么
单例模式(Singleton Pattern):创建型模式,提供了一种创建对象的最佳方式,这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建
在应用程序运行期间,单例模式只会在全局作用域下创建一次实例对象,让所有需要调用的地方都共享这一单例对象,如下图所示:
从定义上来看,全局变量好像就是单例模式,但是一般情况我们不认为全局变量是一个单例模式,原因是:
二、实现
在
javascript
中,实现一个单例模式可以用一个变量来标志当前的类已经创建过对象,如果下次获取当前类的实例时,直接返回之前创建的对象即可,如下:使用闭包也能够实现,如下:
也可以将上述的方法稍作修改,变成构造函数的形式,如下:
三、使用场景
在前端中,很多情况都是用到单例模式,例如页面存在一个模态框的时候,只有用户点击的时候才会创建,而不是加载完成之后再创建弹窗和隐藏,并且保证弹窗全局只有一个
可以先创建一个通常的获取对象的方法,如下:
创建弹窗的代码如下:
上述这种实现称为惰性单例,意图解决需要时才创建类实例对象
并且
Vuex
、redux
全局态管理库也应用单例模式的思想,如下图:现在很多第三方库都是单例模式,多次引用只会使用同一个对象,如
jquery
、lodash
、moment
...参考文献
The text was updated successfully, but these errors were encountered: