Singleton 模式确保一个类只有一个实例,并提供全局访问点。当恰好需要一个对象来协调某件事情(配置、连接池、日志记录器)时使用它,但这在某种程度上也很有争议。
Singleton 做什么
text
SINGLETON → guarantee a class has only ONE instance, with global access to it:
→ the class controls its own instantiation (private constructor)
→ returns the SAME instance every time it's requested
→ for: things there should only be ONE of, accessed from many places
示例
js
{
#instance;
() {
(!.#instance) {
.#instance = ();
}
.#instance;
}
}
