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

30 lines
613 B
Markdown

---
nav:
title: 前端
path: /fea
group:
title: 💊 设计模式
order: 2
---
## 单例模式
一个类只有一个实例,并提供一个访问它的全局访问点。
```js
// ------------------- 单例模式 -------------------
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
}
}
```