A class with only a single instance with global access points. 保证类仅仅只有一个实例,并且只提供一个访问它的全局点。
适用:
- 类只能够有一个实例,切客户只能够从一个众所周知的访问点访问它的时候。
- 当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能够用一个扩展的实例时。
效果:
- 对唯一实例的受控访问。
- 缩小命名空间。
- 允许对操作和表示的精细化。
- 允许可变数目的实例。
- 比类操作更灵活。
let LazySingle = (function () {let instance = null;function Single() {return {getter: () => {},setter: () => {}}}return function () {if (instance === null) {instance = new Single();}return instance}})();
or the modern one
let appInstance = null;class App {constructor() {if (appInstance) return appInstance;// ...appInstance = this;}}export default App;
