JavaScript ES6 中的单例写法

本文最后更新于:2022年10月12日 下午

Singleton Pattern

单例类

该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

  • 单例类只能有一个实例。
  • 单例类必须自己创建自己的唯一实例。
  • 单例类必须给所有其他对象提供这一实例。

ES6中创建单例模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class SingletonApple {
constructor(name, creator, products) {
this.name = name;
this.creator = creator;
this.products = products;
}
//静态方法
static getInstance(name, creator, products) {
if(!this.instance) {
this.instance = new SingletonApple(name, creator, products);
}
return this.instance;
}
}

从ES6重新认识JavaScript设计模式(一): 单例模式 - 知乎 (zhihu.com)


JavaScript ES6 中的单例写法
https://elcfin.github.io/2021/11/09/JavaScript 中的单例写法/
作者
Elcfin
发布于
2021年11月9日
许可协议