/**
- 教程级别
*/
class Singleton1 {
private static _ins: Singleton1
private constructor() { }
static get Ins() {
if (!this._ins) {
this._ins = new Singleton1()
}
return this._ins
}
}
/**
- 优化写法
*/
class Singleton2 {
private static _ins: Singleton1
private constructor() { }
static get Ins() {
if (this._ins) {
return this._ins
}
this._ins = new Singleton2()
return this._ins
}
}
/**
- 行数最少最简单的写法
*/
class Singleton3 {
private static _ins: Singleton3
private constructor() { }
static get Ins() { return this._ins ?? (this._ins = new Singleton3()) }
}