什么是单例模式

  单例模式 (Singleton Pattern)又称为单体模式,保证一个类只有一个实例,并提供一个访问它的全局访问点。也就是说,第二次使用同一个类创建新对象的时候,应该得到与第一次创建的对象完全相同的对象。

简单的说就是保证一个类仅有一个实例,并提供一个访问它的全局访问点,这样的模式就叫做单例模式。

单例模式的实现思路

现在我们先不考虑单例模式的应用场景,单看它的实现,思考这样一个问题:如何才能保证一个类仅有一个实例?
一般情况下,当我们创建了一个类(本质是构造函数)后,可以通过new关键字调用构造函数可以生成任意多的实例对象,像这样:

class Single {
show() {
console.log('我是一个单例对象')
}
}
const s1 = new Single()
const s2 = new Single()
// false
s1 === s2

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

要做到这一点,就需要构造函数具备判断自己是否已经创建过一个实例的能力,那么只要用一个变量保存创建的实例,后面判断这个变量即可。

js实现

// es6
class SingletonApple {
static instance = null;
constructor(name, creator, products) {
this.name = name;
this.creator = creator;
this.products = products;
if (!SingletonApple.instance) {
SingletonApple.instance = this;
}
return SingletonApple.instance;
}
//静态方法
static getInstance(name, creator, products) {
if (!this.instance) {
this.instance = new SingletonApple(name, creator, products);
}
return this.instance;
}
}
console.log(SingletonApple.instance);
let ss = new SingletonApple("苹果公司", "阿辉",
["iPhone", "iMac", "iPad", "iPod"]);
console.log(ss);
console.log(ss === SingletonApple.instance);
let appleCompany = SingletonApple.getInstance("苹果公司", "乔布斯", [
"iPhone",
"iMac",
"iPad",
"iPod"
]);
let copyApple = SingletonApple.getInstance("苹果公司", "阿辉", [
"iPhone",
"iMac",
"iPad",
"iPod"
]);
console.log(appleCompany === copyApple); //true
// 闭包-通过自执行函数创建闭包变量
const Singleton = (function () {
const SingleClass = function (name, creator, products) {
this.name = name;
this.creator = creator;
this.products = products;
};
let instance = null;
return function (name, creator, products) {
if (!instance) {
// 如果不存在 则new一个
instance = new SingleClass(name, creator, products)
}
return instance;
}
})()
let s1 = new Singleton("单例1", "阿辉1", [
"iPhone",
"iMac",
"iPad",
"iPod"])
let s2 = new Singleton("单例2", "阿辉2", [
"iPhone",
"iMac",
"iPad",
"iPod"])
console.log(s1 === s2)
console.log(s1)

注意点

  1. 确保只有一个实例
  2. 并提供全局访问
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。